aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhjl <hjl@138bc75d-0d04-0410-961f-82ee72b054a4>2009-03-27 12:52:52 +0000
committerhjl <hjl@138bc75d-0d04-0410-961f-82ee72b054a4>2009-03-27 12:52:52 +0000
commitd924b67076075c74e1bc95afa3e6b83bc087a4dc (patch)
treea838c5940ee7e4cdb7c09c0bf3ae71d1c2c5d538
parent9d72fd698d27798d012a7833b7490ae65cd7163b (diff)
gcc/
2009-03-27 Manuel Lopez-Ibanez <manu@gcc.gnu.org> PR c++/35652 * builtins.h (c_strlen): Do not warn here. * c-typeck.c (build_binary_op): Adjust calls to pointer_int_sum. * c-common.c (pointer_int_sum): Take an explicit location. Warn about offsets out of bounds. * c-common.h (pointer_int_sum): Adjust declaration. gcc/cp/ 2009-03-27 Manuel Lopez-Ibanez <manu@gcc.gnu.org> PR c++/35652 * typeck.c (cp_pointer_sum): Adjust call to pointer_int_sum. gcc/testsuite/ 2009-03-27 Manuel Lopez-Ibanez <manu@gcc.gnu.org> PR c++/35652 * gcc.dg/pr35652.C: New. * g++.dg/warn/pr35652.C: New. * gcc.dg/format/plus-1.c: Adjust message. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@145102 138bc75d-0d04-0410-961f-82ee72b054a4
-rw-r--r--gcc/ChangeLog13
-rw-r--r--gcc/builtins.c13
-rw-r--r--gcc/c-common.c34
-rw-r--r--gcc/c-common.h2
-rw-r--r--gcc/c-typeck.c6
-rw-r--r--gcc/cp/ChangeLog5
-rw-r--r--gcc/cp/typeck.c2
-rw-r--r--gcc/testsuite/ChangeLog9
-rw-r--r--gcc/testsuite/g++.dg/warn/pr35652.C30
-rw-r--r--gcc/testsuite/gcc.dg/format/plus-1.c3
-rw-r--r--gcc/testsuite/gcc.dg/pr35652.c13
11 files changed, 110 insertions, 20 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index f5b7076b8c4..fbb0ae5bdf6 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,7 +1,16 @@
+2009-03-27 Manuel Lopez-Ibanez <manu@gcc.gnu.org>
+
+ PR c++/35652
+ * builtins.h (c_strlen): Do not warn here.
+ * c-typeck.c (build_binary_op): Adjust calls to pointer_int_sum.
+ * c-common.c (pointer_int_sum): Take an explicit location.
+ Warn about offsets out of bounds.
+ * c-common.h (pointer_int_sum): Adjust declaration.
+
2009-03-26 Ralf Wildenhues <Ralf.Wildenhues@gmx.de>
- * doc/invoke.texi (i386 and x86-64 Windows Options): Fix texinfo
- markup glitch.
+ * doc/invoke.texi (i386 and x86-64 Windows Options): Fix texinfo
+ markup glitch.
2009-03-26 Jakub Jelinek <jakub@redhat.com>
diff --git a/gcc/builtins.c b/gcc/builtins.c
index cc9d93e2311..929ea9069cb 100644
--- a/gcc/builtins.c
+++ b/gcc/builtins.c
@@ -469,16 +469,13 @@ c_strlen (tree src, int only_value)
else
offset = tree_low_cst (offset_node, 0);
- /* If the offset is known to be out of bounds, warn, and call strlen at
- runtime. */
+ /* If the offset is known to be out of bounds, the front-end should
+ have warned already. We call strlen at runtime.
+
+ ??? Perhaps we should turn this into an assert and force
+ front-ends to define offsets whtin boundaries. */
if (offset < 0 || offset > max)
{
- /* Suppress multiple warnings for propagated constant strings. */
- if (! TREE_NO_WARNING (src))
- {
- warning (0, "offset outside bounds of constant string");
- TREE_NO_WARNING (src) = 1;
- }
return NULL_TREE;
}
diff --git a/gcc/c-common.c b/gcc/c-common.c
index cc00511cc0c..317c1d7db63 100644
--- a/gcc/c-common.c
+++ b/gcc/c-common.c
@@ -3211,7 +3211,8 @@ shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
of pointer PTROP and integer INTOP. */
tree
-pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
+pointer_int_sum (location_t location, enum tree_code resultcode,
+ tree ptrop, tree intop)
{
tree size_exp, ret;
@@ -3220,19 +3221,19 @@ pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
{
- pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
+ pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
"pointer of type %<void *%> used in arithmetic");
size_exp = integer_one_node;
}
else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
{
- pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
+ pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
"pointer to a function used in arithmetic");
size_exp = integer_one_node;
}
else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
{
- pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
+ pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
"pointer to member function used in arithmetic");
size_exp = integer_one_node;
}
@@ -3295,6 +3296,31 @@ pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
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);
+ 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 ();
diff --git a/gcc/c-common.h b/gcc/c-common.h
index 475c3334592..6ba33c6aedc 100644
--- a/gcc/c-common.h
+++ b/gcc/c-common.h
@@ -746,7 +746,7 @@ extern tree shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwis
and, if so, perhaps change them both back to their original type. */
extern tree shorten_compare (tree *, tree *, tree *, enum tree_code *);
-extern tree pointer_int_sum (enum tree_code, tree, tree);
+extern tree pointer_int_sum (location_t, enum tree_code, tree, tree);
/* Add qualifiers to a type, in the fashion for C. */
extern tree c_build_qualified_type (tree, int);
diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c
index f1d83f3c20c..83188baf22f 100644
--- a/gcc/c-typeck.c
+++ b/gcc/c-typeck.c
@@ -8107,12 +8107,12 @@ build_binary_op (location_t location, enum tree_code code,
/* Handle the pointer + int case. */
if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
{
- ret = pointer_int_sum (PLUS_EXPR, op0, op1);
+ ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
goto return_build_binary_op;
}
else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
{
- ret = pointer_int_sum (PLUS_EXPR, op1, op0);
+ ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
goto return_build_binary_op;
}
else
@@ -8131,7 +8131,7 @@ build_binary_op (location_t location, enum tree_code code,
/* Handle pointer minus int. Just like pointer plus int. */
else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
{
- ret = pointer_int_sum (MINUS_EXPR, op0, op1);
+ ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
goto return_build_binary_op;
}
else
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index bac3d7a7a8d..04977ae5466 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,8 @@
+2009-03-27 Manuel Lopez-Ibanez <manu@gcc.gnu.org>
+
+ PR c++/35652
+ * typeck.c (cp_pointer_sum): Adjust call to pointer_int_sum.
+
2009-03-26 Andrew Haley <aph@redhat.com>
PR C++/39380
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index a6986f94b63..3788a7e8b9e 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -4031,7 +4031,7 @@ cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
pointer_int_sum() anyway. */
complete_type (TREE_TYPE (res_type));
- return pointer_int_sum (resultcode, ptrop,
+ return pointer_int_sum (input_location, resultcode, ptrop,
fold_if_not_in_template (intop));
}
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index e3e6ab4932d..ea4b9c34a0c 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,10 @@
+2009-03-27 Manuel Lopez-Ibanez <manu@gcc.gnu.org>
+
+ PR c++/35652
+ * gcc.dg/pr35652.C: New.
+ * g++.dg/warn/pr35652.C: New.
+ * gcc.dg/format/plus-1.c: Adjust message.
+
2009-03-26 Jakub Jelinek <jakub@redhat.com>
PR c++/39554
@@ -10,7 +17,7 @@
2009-03-25 Alexander Monakov <amonakov@ispras.ru>
- * gcc.target/ia64/20090324-1.c: New test.
+ * gcc.target/ia64/20090324-1.c: New test.
2009-03-25 Jakub Jelinek <jakub@redhat.com>
diff --git a/gcc/testsuite/g++.dg/warn/pr35652.C b/gcc/testsuite/g++.dg/warn/pr35652.C
new file mode 100644
index 00000000000..7ce9431eb2b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/pr35652.C
@@ -0,0 +1,30 @@
+// PR c++/35652: wrong location and duplicated warning.
+// { dg-do compile }
+// { dg-options "-fshow-column" }
+#include <string>
+int foo() {
+ // blank line padding, could also be code...
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ std::string s = "";
+ s += 'x' + "y"; // { dg-warning "14:offset '120' outside bounds of constant string" }
+ // { dg-bogus "offset '120' outside bounds of constant string.*offset '120' outside bounds of constant string" "duplicated" { target *-*-* } 17 }
+}
+
+int bar()
+{
+ const char *s = 'z' + "y"; /* { dg-warning "25:offset '122' outside bounds of constant string" } */
+}
+
+int g()
+{
+ char str[2];
+ const char *p = str + sizeof(str);
+}
diff --git a/gcc/testsuite/gcc.dg/format/plus-1.c b/gcc/testsuite/gcc.dg/format/plus-1.c
index 02a213d417d..0d8b62cd3c5 100644
--- a/gcc/testsuite/gcc.dg/format/plus-1.c
+++ b/gcc/testsuite/gcc.dg/format/plus-1.c
@@ -15,6 +15,9 @@ foo (int i)
printf (3 + "%d\n"); /* { dg-warning "zero-length" "zero-length string" } */
printf ("%d\n" + i, i); /* { dg-warning "not a string" "non-constant addend" } */
printf ("%d\n" + 10); /* { dg-warning "not a string" "too large addend" } */
+ /* { dg-warning "offset '10' outside bounds of constant string" "offset" { target *-*-* } 17 } */
printf ("%d\n" - 1, i); /* { dg-warning "not a string" "minus constant" } */
+ /* { dg-warning "offset '-1' outside bounds of constant string" "offset" { target *-*-* } 19 } */
printf ("%d\n" + -1, i); /* { dg-warning "not a string" "negative addend" } */
+ /* { dg-warning "offset '-1' outside bounds of constant string" "offset" { target *-*-* } 21 } */
}
diff --git a/gcc/testsuite/gcc.dg/pr35652.c b/gcc/testsuite/gcc.dg/pr35652.c
new file mode 100644
index 00000000000..50ec3acf10a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr35652.c
@@ -0,0 +1,13 @@
+/* PR c++/35652: wrong location and duplicated warning.
+ { dg-do compile }
+ { dg-options "" } */
+int bar()
+{
+ const char *s = 'z' + "y"; /* { dg-warning "offset '122' outside bounds of constant string" } */
+}
+
+int g()
+{
+ char str[2];
+ const char *p = str + sizeof(str);
+}