public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] avoid assuming gimple_call_alloc_size argument is a call (PR 99489)
@ 2021-03-09 22:07 Martin Sebor
  2021-03-12 13:52 ` Jakub Jelinek
  0 siblings, 1 reply; 5+ messages in thread
From: Martin Sebor @ 2021-03-09 22:07 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 440 bytes --]

The gimple_call_alloc_size() function is documented to "return null
when STMT is not a call to a valid allocation function" but the code
assumes STMT is a call statement, causing the function to ICE when
it isn't.

The attached patch changes the function to fulfill its contract and
return null also when STMT isn't a call.  The fix seems obvious to
me but I'll wait some time before committing it in case it's not
to someone else.

Martin

[-- Attachment #2: gcc-99489.diff --]
[-- Type: text/x-patch, Size: 1919 bytes --]

PR tree-optimization/99489 - ICE calling strncat after strcat

gcc/ChangeLog:

	PR tree-optimization/99489
	* builtins.c (gimple_call_alloc_size): Fail gracefully when argument
	is not a call statement.

gcc/testsuite/ChangeLog:

	PR tree-optimization/99489
	* gcc.dg/Wstringop-truncation-9.c: New test.

diff --git a/gcc/builtins.c b/gcc/builtins.c
index 41e336c071c..196dda3fa5e 100644
--- a/gcc/builtins.c
+++ b/gcc/builtins.c
@@ -4924,7 +4924,7 @@ tree
 gimple_call_alloc_size (gimple *stmt, wide_int rng1[2] /* = NULL */,
 			range_query * /* = NULL */)
 {
-  if (!stmt)
+  if (!stmt || !is_gimple_call (stmt))
     return NULL_TREE;
 
   tree allocfntype;
diff --git a/gcc/testsuite/gcc.dg/Wstringop-truncation-9.c b/gcc/testsuite/gcc.dg/Wstringop-truncation-9.c
new file mode 100644
index 00000000000..63614809da4
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wstringop-truncation-9.c
@@ -0,0 +1,41 @@
+/* PR tree-optimization/99489 - ICE calling strncat after strncat
+   { dg-do compile }
+   { dg-options "-O2 -Wall" } */
+
+// Important -- see pr82429.
+char *stpcpy (char *, const char *);
+
+void fchar (char *d, char c, char *s)
+{
+  __builtin_strcat (d, s);
+  __builtin_strncat (d, &c, 1);
+}
+
+void fcstchar (char *d, char *s)
+{
+  __builtin_strcat (d, s);
+
+  const char c = 'x';
+  __builtin_strncat (d, &c, 1);     // { dg-warning "-Wstringop-truncation" }
+}
+
+void fstr (char *d, char *s)
+{
+  __builtin_strcat (d, s);
+  __builtin_strncat (d, s, 1);
+}
+
+void farr (char *d, char *s)
+{
+  __builtin_strcat (d, s);
+
+  char a[] = "x";
+  __builtin_strncat (d, a, 1);      // { dg-warning "-Wstringop-truncation" }
+}
+
+void flit (char *d, char *s)
+{
+  __builtin_strcat (d, s);
+  __builtin_strncat (d, "x", 1);    // { dg-warning "-Wstringop-truncation" "pr?????" { xfail *-*-*} }
+                                    // { dg-warning "-Wstringop-overflow" "actual" { target *-*-*} .-1 }
+}

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-03-13 21:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-09 22:07 [PATCH] avoid assuming gimple_call_alloc_size argument is a call (PR 99489) Martin Sebor
2021-03-12 13:52 ` Jakub Jelinek
2021-03-12 14:04   ` David Malcolm
2021-03-12 14:11     ` Jakub Jelinek
2021-03-13 21:31   ` Martin Sebor

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).