]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
expand: Don't depend on warning flags in code generation of strnlen [PR94189]
authorJakub Jelinek <jakub@redhat.com>
Tue, 17 Mar 2020 09:42:35 +0000 (10:42 +0100)
committerJakub Jelinek <jakub@redhat.com>
Tue, 17 Mar 2020 17:25:26 +0000 (18:25 +0100)
The following testcase FAILs with -O2 -fcompare-debug, but the reason isn't
that we'd emit different code based on -g or non-debug, but rather that
we emit different code depending on whether -w is used or not (or e.g.
-Wno-stringop-overflow or whether some other pass emitted some other warning
already on the call).

Code generation shouldn't depend on whether we emit a warning or not if at
all possible.

The following patch punts (i.e. doesn't optimize the strnlen call to a
constant value) if we would emit the warning if it was enabled.
In the PR there is an alternate patch which does optimize the strnlen call
no matter if we emit the warning or not, though I think I prefer the version
below, e.g. the strnlen call might be crossing field boundaries, which is in
strict reading undefined, but I'd be afraid people do that in the real
world programs.

2020-03-17  Jakub Jelinek  <jakub@redhat.com>

PR middle-end/94189
* builtins.c (expand_builtin_strnlen): Do return NULL_RTX if we would
emit a warning if it was enabled and don't depend on TREE_NO_WARNING
for code-generation.

* gcc.dg/pr94189.c: New test.

gcc/ChangeLog
gcc/builtins.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/pr94189.c [new file with mode: 0644]

index 6e05c9699938a45394d3d3e9cb9c533a48f38bc1..d443cb45ed434d48c4864e9d43ec6e73c34782cc 100644 (file)
@@ -1,5 +1,10 @@
 2020-03-17  Jakub Jelinek  <jakub@redhat.com>
 
+       PR middle-end/94189
+       * builtins.c (expand_builtin_strnlen): Do return NULL_RTX if we would
+       emit a warning if it was enabled and don't depend on TREE_NO_WARNING
+       for code-generation.
+
        Backported from mainline
        2020-03-16  Jakub Jelinek  <jakub@redhat.com>
 
index ed11f79ff0b57f835a8837c88cad512d404954d2..e8e43f53ec689501f7159e37c04e217788300490 100644 (file)
@@ -3112,27 +3112,25 @@ expand_builtin_strnlen (tree exp, rtx target, machine_mode target_mode)
            return NULL_RTX;
        }
 
-      if (lendata.decl
-         && !TREE_NO_WARNING (exp)
-         && ((tree_int_cst_lt (len, bound))
-             || !exact))
+      if (lendata.decl && (tree_int_cst_lt (len, bound) || !exact))
        {
          location_t warnloc
            = expansion_point_location_if_in_system_header (loc);
 
-         if (warning_at (warnloc, OPT_Wstringop_overflow_,
-                         exact
-                         ? G_("%K%qD specified bound %E exceeds the size %E "
-                              "of unterminated array")
-                         : G_("%K%qD specified bound %E may exceed the size "
-                              "of at most %E of unterminated array"),
-                         exp, func, bound, len))
+         if (!TREE_NO_WARNING (exp)
+             && warning_at (warnloc, OPT_Wstringop_overflow_,
+                            exact
+                            ? G_("%K%qD specified bound %E exceeds the size "
+                                 "%E of unterminated array")
+                            : G_("%K%qD specified bound %E may exceed the "
+                                 "size of at most %E of unterminated array"),
+                            exp, func, bound, len))
            {
              inform (DECL_SOURCE_LOCATION (lendata.decl),
                      "referenced argument declared here");
              TREE_NO_WARNING (exp) = true;
-             return NULL_RTX;
            }
+         return NULL_RTX;
        }
 
       if (!len)
index 522a7608203d4363462042a26a0e858609dc77fd..f982ee6a9ecb9c9d8c5823bb9a89e5e9536493fe 100644 (file)
@@ -1,5 +1,8 @@
 2020-03-17  Jakub Jelinek  <jakub@redhat.com>
 
+       PR middle-end/94189
+       * gcc.dg/pr94189.c: New test.
+
        Backported from mainline
        2020-03-16  Jakub Jelinek  <jakub@redhat.com>
 
diff --git a/gcc/testsuite/gcc.dg/pr94189.c b/gcc/testsuite/gcc.dg/pr94189.c
new file mode 100644 (file)
index 0000000..f927d55
--- /dev/null
@@ -0,0 +1,11 @@
+/* PR middle-end/94189 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fcompare-debug" } */
+
+const char a[] = { 'a', 'b', 'c', 'd' };/* { dg-message "declared here" } */
+
+int
+foo (void)
+{
+  return __builtin_strnlen (a, 5);     /* { dg-warning "specified bound 5 exceeds the size 4 of unterminated array" } */
+}