facebook / facebook/folly

[MSVC] Warning C4146 from compiler about implementation of VectorContainerPolicy::valuesOffset

Open
#2,406 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

MSVC++ compiler produces warning [C4146](https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4146?view=msvc-170) for this code line: https://github.com/facebook/folly/blob/a16cabf797d3f5acfe88a7bcd546d1b3ff407b0f/folly/container/detail/F14Policy.h#L1328

It's because `prefixBytes` is unsigned `std::size_t` and there is an attemp to change the sign for unsigned value.

May be this code line has to be rewritten such way:
```cpp
prefixBytes = static_cast( -(-static_cast(prefixBytes) & ~(alignof(Value) - 1)) );
```
or:
```cpp
const auto signedPrefixBytes = static_cast(prefixBytes);
prefixBytes = static_cast( -(-signedPrefixBytes & ~(alignof(Value) - 1)) );
```

Contributor guide

Open the contributing guide

Research direction

Start in folly/container/detail/F14Policy.h at the valuesOffset implementation around line 1328 and reproduce the MSVC C4146 warning. Compare the existing unsigned arithmetic with the proposed signed conversions, then verify that the warning is gone and the offset behavior is preserved in the relevant MSVC build.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.