------- Comment #8 from manu at gcc dot gnu dot org  2008-11-01 17:44 -------
This is my current patch and it works in this testcase. However, it also
triggers on cases like: const char *p = str + sizeof(str)

Perhaps I am doing this at the wrong place. Any suggestions?


@@ -3322,10 +3323,36 @@ pointer_int_sum (enum tree_code resultco

   /* Create the sum or difference.  */
   if (resultcode == MINUS_EXPR)
     intop = fold_build1 (NEGATE_EXPR, sizetype, intop);

+
+  if (TREE_CODE (intop) == INTEGER_CST)
+    {
+      tree offset_node;
+      tree string_cst = string_constant (ptrop, &offset_node);
+
+      if (string_cst != 0
+         && !(offset_node && TREE_CODE (offset_node) != INTEGER_CST))
+       {
+         HOST_WIDE_INT max = TREE_STRING_LENGTH (string_cst) - 1;
+         HOST_WIDE_INT offset;
+         if (offset_node == 0)
+           offset = 0;
+         else if (! host_integerp (offset_node, 0))
+           offset = -1;
+         else
+           offset = tree_low_cst (offset_node, 0);
+
+         offset = offset + tree_low_cst (intop, 0);
+         if (offset < 0 || offset > max)
+           warning_at (location, 0,
+                       "offset %<%ld%> outside bounds of constant string",
+                       tree_low_cst (intop, 0));
+       }
+    }
+
   ret = fold_build2 (POINTER_PLUS_EXPR, result_type, ptrop, intop);

   fold_undefer_and_ignore_overflow_warnings ();

   return ret;


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35652

Reply via email to