diff mbox series

[committed] avoid null dereference in maybe_warn_nonstring_arg (PR 88659)

Message ID 2851d3ff-1be8-7add-3e48-673bd6d84d02@gmail.com
State New
Headers show
Series [committed] avoid null dereference in maybe_warn_nonstring_arg (PR 88659) | expand

Commit Message

Martin Sebor Jan. 4, 2019, 3:21 a.m. UTC
The attached patch avoids dereferencing a null pointer in
maybe_warn_nonstring_arg reported to cause an ICE in pr88659.
Committed in r267569 after bootstrapping and regtesting on
x86_64-linux.

Martin
diff mbox series

Patch

gcc/ChangeLog:

	PR tree-optimization/88659
        * calls.c (maybe_warn_nonstring_arg): Avoid assuming maxlen is set.

gcc/testsuite/ChangeLog:

	PR tree-optimization/88659
        * gcc.dg/Wstringop-truncation-6.c: New test.

Index: gcc/calls.c
===================================================================
--- gcc/calls.c	(revision 267568)
+++ gcc/calls.c	(working copy)
@@ -1681,7 +1681,7 @@  maybe_warn_nonstring_arg (tree fndecl, tree exp)
 	  bndrng[1] = maxlen;
 	  bound = void_type_node;
 	}
-      else
+      else if (maxlen)
 	{
 	  /* Replace the bound on the operation with the upper bound
 	     of the length of the string if the latter is smaller.  */
Index: gcc/testsuite/gcc.dg/Wstringop-truncation-6.c
===================================================================
--- gcc/testsuite/gcc.dg/Wstringop-truncation-6.c	(nonexistent)
+++ gcc/testsuite/gcc.dg/Wstringop-truncation-6.c	(working copy)
@@ -0,0 +1,42 @@ 
+/* PR tree-optimization/88659 - ICE in maybe_warn_nonstring_arg
+   { dg-do compile }
+   { dg-options "-O0 -Wall" }  */
+
+const char a[5] = "1234";
+
+int cst_idx_cst_bnd (void)
+{
+  return __builtin_strnlen (&a[1], 0);
+}
+
+int var_idx_cst_bnd (void)
+{
+  int i = 1;
+  return __builtin_strnlen (&a[i], 0);
+}
+
+int phi_idx_cst_bnd (int i)
+{
+  return __builtin_strnlen (&a[i ? 1 : 2], 0);
+}
+
+int unk_idx_cst_bnd (int i)
+{
+  return __builtin_strnlen (&a[i], 0);
+}
+
+int cst_idx_var_bnd (void)
+{
+  int n = 0;
+  return __builtin_strnlen (&a[1], n);
+}
+
+int cst_idx_phi_bnd (int n)
+{
+  return __builtin_strnlen (&a[1], n ? 1 : 2);
+}
+
+int cst_idx_unk_bnd (int n)
+{
+  return __builtin_strnlen (&a[1], n);
+}