This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug middle-end/81483] New: spurious -Wformat-overflow warning for limited types


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81483

            Bug ID: 81483
           Summary: spurious -Wformat-overflow warning for limited types
           Product: gcc
           Version: 7.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: arnd at linaro dot org
  Target Milestone: ---

After looking in more detail at -Wformat-overflow warnings in the linux
kernels, I managed to reduce one warning to this test case:

#include <stdio.h>
void alienware_zone_init(unsigned char num_zones)
{
        unsigned char zone;
        char buffer[10];

        for (zone = 0; zone < num_zones; zone++)
                sprintf(buffer, "zone%02X", zone);
}

I get this warning:

$ x86_64-linux-gcc-7.1.1 -Wall -S /tmp/test4.c -O2 
/tmp/test4.c: In function 'alienware_zone_init':
/tmp/test4.c:9:24: warning: '%02X' directive writing between 2 and 8 bytes into
a region of size 6 [-Wformat-overflow=]
   sprintf(buffer, "zone%02X", zone);
                        ^~~~
/tmp/test4.c:9:19: note: directive argument in the range [0, 2147483647]
   sprintf(buffer, "zone%02X", zone);
                   ^~~~~~~~~~
/tmp/test4.c:9:3: note: 'sprintf' output between 7 and 13 bytes into a
destination of size 10
   sprintf(buffer, "zone%02X", zone);
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

which is surprising because the range "[0, 2147483647]" is larger
than an 'unsigned char' can hold. In similar test cases that use
an 'unsigned char' without the loop, we do not get the warning.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]