printf icon indicating copy to clipboard operation
printf copied to clipboard

GCC 13.2 warns about -Wmaybe-uninitialized when compiling with -Ofast

Open sylt opened this issue 1 year ago • 7 comments

First of all, this is not reproducible on the develop branch as of writing this.

How to reproduce (on the current master):

$ git clone https://github.com/eyalroz/printf.git
$ cd printf/
$ gcc -c -o /dev/null -Wall -Isrc src/printf/printf.c -Ofast

Yielding the output:

In function ‘apply_scaling’,
    inlined from ‘get_normalized_components’ at src/printf/printf.c:706:29,
    inlined from ‘print_exponential_number’ at src/printf/printf.c:964:5:
src/printf/printf.c:658:66: warning: ‘normalization.raw_factor’ may be used uninitialized [-Wmaybe-uninitialized]
  658 |   return normalization.multiply ? num * normalization.raw_factor : num / normalization.raw_factor;
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/printf/printf.c: In function ‘print_exponential_number’:
src/printf/printf.c:904:25: note: ‘normalization.raw_factor’ was declared here
  904 |   struct scaling_factor normalization;
      |                         ^~~~~~~~~~~~~

I've only seen this on GCC 13.2 (no warning on 9, 11, nor 12), compiling for/on x86-64.

If one doesn't need support for the specifiers %[egEG], the easiest way to work around it is to add -DPRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS=0 when compiling.

sylt avatar Sep 12 '24 12:09 sylt

Dupe of #167 I believe, which is resolved on the development branch. So, closing...

eyalroz avatar Sep 12 '24 19:09 eyalroz

@eyalroz Thanks for having a look! Perhaps I'm mistaken though but I don't think it's a dupe: The fix for #167 is, according to git log, already present on master. Therefore, in my reproduction steps (which is based on master), I don't think I should see it if actually was a dupe, or?

sylt avatar Sep 13 '24 07:09 sylt

You are right, actually.

I've run the command-line you've provided. Interestingly, one gets the error with gcc-13 (13.3.0); but with gcc-14 (14.2.0) there is no warning!

Also, I now notice that it's a different line than the one I added the fix for in #167. So, not a dupe, but still already resolved on development.

eyalroz avatar Sep 13 '24 09:09 eyalroz

Hmm, maybe I misunderstood the bug process, I thought that bugs fixed on development (but are valid on master) were kept open, but with the added label "resolved-on-develop" ?

sylt avatar Sep 13 '24 09:09 sylt

Well, you didn't misunderstand, but since it's a compiler bug, I don't think it makes sense to keep this open. But I'm kind of wishy-washy, so let's say it's open-and-resolved-on-dev

eyalroz avatar Sep 13 '24 09:09 eyalroz

I'm having troubles with this on Windows, cl (VS2022) throws: C:\github\printf\src\printf\printf.c(956): warning C4701: potentially uninitialized local variable 'abs_exp10_covered_by_powers_table' used

Out of curiosity, how much troubles would it cause to simply initialize the variable instead of disabling the warning? Let's say in here:

   // Determine the decimal exponent
  if (abs_number == (floating_point_t) 0.0) {
    // TODO: This is a special-case for 0.0 (and -0.0); but proper handling is required for denormals more generally.
    floored_exp10 = 0; // ... and no need to set a normalization factor or check the powers table
    abs_exp10_covered_by_powers_table = false; // <-- HERE
  }

yzazik avatar Feb 19 '25 17:02 yzazik

@yzazik : This looks like a separate bug, please file it separately.

eyalroz avatar Feb 19 '25 22:02 eyalroz