facebook / facebook/folly

Deadlock-safe lock function in Synchronized.h causes avoidable compiler warnings due to signed/unsigned integer comparison

Open
#1,768 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
30.5k
Forks
5.9k
PR merge metrics
No merged PRs in 30d

Description

The `lock` function in `Synchronized.h` that takes multiple lockers produces compiler warnings when compiled with g++ and -Wsign-compare (included in -Wall), due to the comparison between signed and unsigned values in the `for_each` callback.

The following code will not compile with g++ 9.4.0 when using the `-Wall -Werror -std=c++17` flags on Ubuntu 20.04

```c++
#include

int main()
{
folly::Synchronized a;
folly::Synchronized b;

auto [aptr, bptr] = lock(folly::wlock(a), folly::wlock(b));
*aptr = 1;
*bptr = 2;
}
```

due to the declaration `auto indexLocked = 0;` of `indexLocked` as a signed integer, and the subsequent comparison `if (index != indexLocked)` against the unsigned `index` argument to the `for_each` callback.

This can be fixed by explicitly declaring `indexLocked` as an unsigned type.

Contributor guide

Open the contributing guide

Research direction

Start in Synchronized.h at the multi-locker lock function and inspect the for_each callback around indexLocked and the index comparison. Reproduce the example with g++ 9.4.0 and -Wall -Werror -std=c++17; the work is done when this code compiles without the signed/unsigned comparison warning.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.