This is the mail archive of the gcc-patches@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]

[PATCH] prevent -Wall from resetting -Wstringop-overflow=2 to 1 (pr 81345)


The -Wstringop-overflow option defaults to 2 (for Object Size
Checking type 1).  But when -Wall is used it resets the default
value to 1.  This happens because when I added the option to
c.opt I assumed it would default to, well, the default value
set by the Init() directive regardless of whether or not -Wall
was used.  The attached patch explicitly specifies the defaults
to correct this.

Btw., I think this behavior is too surprising to be correct or
(I hope) even intended for options with arguments.  -Wstringop-
overflow is specified like this:

C ObjC C++ ObjC++ Joined RejectNegative UInteger Var(warn_stringop_overflow) Init(2) Warning LangEnabledBy(C ObjC C++ ObjC++, Wall) IntegerRange(0, 4)

with the LangEnabledBy form used above documented like this:

  LangEnabledBy(language, opt)

    When compiling for the given language, the option is set to
    the value of -opt,

IMO, it makes little sense for an option that takes an argument
and that specifies a binary option like -Wall in LangEnabledBy
to default to the binary value of the latter option.  I think
it would be more intuitive and convenient for it to default to
the value set by its Init directive for the positive form of
the binary option and to zero for the negative form (or to empty
for strings, if that's ever done).

Martin
PR other/81345 -  -Wall resets -Wstringop-overflow to 1 from the default 2

gcc/c-family/ChangeLog:

	PR other/81345
	* c.opt (-Wstringop-overflow): Set defaults in LangEnabledBy.

gcc/testsuite/ChangeLog:

	PR other/81345
	* gcc.dg/pr81345.c: New test.


diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 05766c4..e0ad3ab 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -732,7 +732,7 @@ Warn about buffer overflow in string manipulation functions like memcpy
 and strcpy.
 
 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)
+C ObjC C++ ObjC++ Joined RejectNegative UInteger Var(warn_stringop_overflow) Init(2) Warning LangEnabledBy(C ObjC C++ ObjC++, Wall, 2, 0) IntegerRange(0, 4)
 Under the control of Object Size type, warn about buffer overflow in string
 manipulation functions like memcpy and strcpy.
 
diff --git a/gcc/testsuite/gcc.dg/pr81345.c b/gcc/testsuite/gcc.dg/pr81345.c
new file mode 100644
index 0000000..c2cbad7
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr81345.c
@@ -0,0 +1,17 @@
+/* PR other/81345 - -Wall resets -Wstringop-overflow to 1 from the default 2
+   { dg-do compile }
+   { dg-options "-O2 -Wall" } */
+
+char a[3];
+
+void f (const char *s)
+{
+  __builtin_strncpy (a, s, sizeof a + 1);   /* { dg-warning "\\\[-Wstringop-overflow=]" } */
+}
+
+struct S { char a[3]; int i; };
+
+void g (struct S *d, const char *s)
+{
+  __builtin_strncpy (d->a, s, sizeof d->a + 1);   /* { dg-warning "\\\[-Wstringop-overflow=]" } */
+}

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