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

            Bug ID: 100401
           Summary: Bogus -Wformat-overflow warning
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lavr at ncbi dot nlm.nih.gov
  Target Milestone: ---

$ gcc --version
gcc (GCC) 10.2.0


$ cat bogus.c
#include <stdio.h>

#define RECLEN  128

struct S {
    int  code;
    char text[0];
};


const void fun(int n)
{
    union {
        struct S s;
        char     rec[RECLEN];
    } x;
    const char* err;

    if (!n) {
        err = "No error";
    } else {
        sprintf(x.s.text, "Error %d", n);
        err = x.s.text;
    }
    printf("%s\n", err);
}


int main(int argc, const char* argv[])
{
    fun(argc);
    return 0;
}


$ gcc -Wall -O2 bogus.c
bogus.c: In function ‘fun’:
bogus.c:22:28: warning: ‘Error ’ directive writing 6 bytes into a region of
size 0 [-Wformat-overflow=]
   22 |         sprintf(x.s.text, "Error %d", n);
      |                            ^~~~~~
bogus.c:22:9: note: ‘sprintf’ output between 8 and 18 bytes into a destination
of size 0
   22 |         sprintf(x.s.text, "Error %d", n);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reply via email to