[libc-commits] [libc] [libc] Fix recently introduced integer-type warnings (PR #125864)

Simon Tatham via libc-commits libc-commits at lists.llvm.org
Wed Feb 5 07:22:30 PST 2025


statham-arm wrote:

Some examples of the warnings:
```
.../libc/src/stdio/printf_core/float_dec_converter_limited.h:365:30: error: implicit conversion loses integer precision: 'int' to 'char' [-Werror,-Wimplicit-int-conversion]
  365 |           output.digits[i] = internal::int_to_b36_char(
      |                            ~ ^~~~~~~~~~~~~~~~~~~~~~~~~~
  366 |               internal::b36_char_to_int(output.digits[i]) + 1);
      |               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```

but this is in fact fine because `b36_int_to_char` always returns something that fits in a `char`, even though it's typed as `int`. So I've wrapped it in `static_cast<int>` to force the compiler to truncate it without complaining, knowing that the truncation won't actually introduce a problem.

```
.../libc/src/stdio/printf_core/float_dec_converter_limited.h:192:45: error: comparison of integers of different signs: 'int' and 'const unsigned int' [-Werror,-Wsign-compare]
  192 |   if (log10_input_max - log10_low_digit + 1 > MAX_DIGITS) {
      |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~
```

but in fact `MAX_DIGITS` also fits in an int, and the compiler can perfectly well check that at compile time since it's `constexpr`. So in this case I've wrapped it in a less verbose constructor call, `int(MAX_DIGITS)`, in the hope that if `MAX_DIGITS` were accidentally redefined too large later, some compiler _might_ still check it.

https://github.com/llvm/llvm-project/pull/125864


More information about the libc-commits mailing list