public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/vendors/redhat/heads/gcc-9-branch)] expand: Don't depend on warning flags in code generation of strnlen [PR94189]
@ 2020-03-17 19:25 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2020-03-17 19:25 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:65de83595faeccd83bc0fefbfb79768f8a3bb2b6

commit 65de83595faeccd83bc0fefbfb79768f8a3bb2b6
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Tue Mar 17 10:42:35 2020 +0100

    expand: Don't depend on warning flags in code generation of strnlen [PR94189]
    
    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.

Diff:
---
 gcc/ChangeLog                  |  5 +++++
 gcc/builtins.c                 | 22 ++++++++++------------
 gcc/testsuite/ChangeLog        |  3 +++
 gcc/testsuite/gcc.dg/pr94189.c | 11 +++++++++++
 4 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 6e05c969993..d443cb45ed4 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -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>
 
diff --git a/gcc/builtins.c b/gcc/builtins.c
index ed11f79ff0b..e8e43f53ec6 100644
--- a/gcc/builtins.c
+++ b/gcc/builtins.c
@@ -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)
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 522a7608203..f982ee6a9ec 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -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
index 00000000000..f927d55279a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr94189.c
@@ -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" } */
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-03-17 19:25 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-17 19:25 [gcc(refs/vendors/redhat/heads/gcc-9-branch)] expand: Don't depend on warning flags in code generation of strnlen [PR94189] Jakub Jelinek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).