Skip to content

Conversation

@inobelar
Copy link

Greetings! I spotted the next warning (using gcc (GCC) 15.1.1 20250425) (in my case with -Werror & other flags for strictness - its an error):

crc32c-main/src/crc32c_round_up.h:19:30: error: unsigned conversion from ‘int’ to ‘uintptr_t’ {aka ‘long unsigned int’} changes value from ‘-4’ to ‘18446744073709551612’ [-Werror=sign-conversion]
   19 |   return (pointer + (N - 1)) & ~(N - 1);
      |          ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~

This warning is caused by implicit signed-to-unsigned conversion when ~(N - 1) is evaluated. The result of the ~ operator on an int may be negative (e.g., ~3 = -4), and converting that to uintptr_t results in a large positive number due to two's complement representation.

That's why I added explicit cast of (N - 1) into uintptr_t before applying the bitwise-not.

Why this fix works and dont break everything:

  • N is of type int, so N - 1 is also an int.
  • ~(N - 1) thus results in an int, which might be negative.
  • When used in the bitwise & with pointer (which is uintptr_t), the int gets implicitly converted to uintptr_t. If it’s negative, the result is a large positive value (e.g., -4 becomes 0xFFFFFFFFFFFFFFFC on a 64-bit system).
  • Casting (N - 1) to uintptr_t before applying ~ ensures the operation is done in unsigned arithmetic, resulting in the correct bitmask without sign-conversion issues.

With this small fix everything was build successfully without any other warnings/errors (on x86_64).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant