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

            Bug ID: 81345
           Summary: -Wall resets -Wstringop-overflow to 1 from the default
                    2
           Product: gcc
           Version: 8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: driver
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

While debugging some new tests of mine I noticed that the
warn_stringop_overflow variable corresponding to the -Wstringop-overflow option
documented to default to 2 is actually set to 1 in the compiler when -Wall
alone is used.  That's wrong and it prevents the option from diagnosing a bunch
of instances of buffer overflow such as those where the destination is a member
array as in the test case below.

The option does appear to be set to 2 in c-family/c.opt as shown below so
something isn't working correctly there:

Wstringop-overflow
C ObjC C++ ObjC++ Warning Alias(Wstringop-overflow=, 2, 0)
...
Wstringop-overflow=
C ObjC C++ ObjC++ Joined RejectNegative UInteger Var(warn_stringop_overflow)
Init(2) Warning LangEnabledBy(C ObjC C++ ObjC++, Wall) IntegerRange(0, 4)


The script below shows that when GCC is invoked with no warning options, both
instances of the warning are diagnosed as expected (that also implies that the
default -Wstringop-overflow=2 is in effect).  The same happens when
-Wstringop-overflow is on the command line.  But when -Wall is on the command
line alone, only one warning is issued because -Wstringop-overflow has been
reset to 1.

$ (set -x && cat t.c && for w in '' -Wstringop-overflow '-Wall'; do
/ssd/build/gcc-git/gcc/xgcc -B /ssd/build/gcc-git/gcc -O2 -S $w t.c; done)+ cat
t.c
char a[3];

void f (const char *s)
{
  __builtin_strncpy (a, s, sizeof a + 1);
}

struct S { char a[3]; int i; };

void g (struct S *d, const char *s)
{
  __builtin_strncpy (d->a, s, sizeof d->a + 1);
}
+ for w in ''\'''\''' -Wstringop-overflow ''\''-Wall'\'''
+ /ssd/build/gcc-git/gcc/xgcc -B /ssd/build/gcc-git/gcc -O2 -S t.c
t.c: In function ‘f’:
t.c:5:3: warning: ‘__builtin_strncpy’ writing 4 bytes into a region of size 3
overflows the destination [-Wstringop-overflow=]
   __builtin_strncpy (a, s, sizeof a + 1);
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
t.c: In function ‘g’:
t.c:12:3: warning: ‘__builtin_strncpy’ writing 4 bytes into a region of size 3
overflows the destination [-Wstringop-overflow=]
   __builtin_strncpy (d->a, s, sizeof d->a + 1);
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ for w in ''\'''\''' -Wstringop-overflow ''\''-Wall'\'''
+ /ssd/build/gcc-git/gcc/xgcc -B /ssd/build/gcc-git/gcc -O2 -S
-Wstringop-overflow t.c
t.c: In function ‘f’:
t.c:5:3: warning: ‘__builtin_strncpy’ writing 4 bytes into a region of size 3
overflows the destination [-Wstringop-overflow=]
   __builtin_strncpy (a, s, sizeof a + 1);
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
t.c: In function ‘g’:
t.c:12:3: warning: ‘__builtin_strncpy’ writing 4 bytes into a region of size 3
overflows the destination [-Wstringop-overflow=]
   __builtin_strncpy (d->a, s, sizeof d->a + 1);
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ for w in ''\'''\''' -Wstringop-overflow ''\''-Wall'\'''
+ /ssd/build/gcc-git/gcc/xgcc -B /ssd/build/gcc-git/gcc -O2 -S -Wall t.c
t.c: In function ‘f’:
t.c:5:3: warning: ‘__builtin_strncpy’ writing 4 bytes into a region of size 3
overflows the destination [-Wstringop-overflow=]
   __builtin_strncpy (a, s, sizeof a + 1);
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reply via email to