aboutsummaryrefslogtreecommitdiff
path: root/gcc/c-common.c
diff options
context:
space:
mode:
authorManuel López-Ibáñez <manu@gcc.gnu.org>2009-03-27 05:52:52 -0700
committerH.J. Lu <hjl@gcc.gnu.org>2009-03-27 05:52:52 -0700
commita5f805df5860bd185c77261deaa48049485bfa4e (patch)
treea838c5940ee7e4cdb7c09c0bf3ae71d1c2c5d538 /gcc/c-common.c
parent9fd1d8548908e5089281e45770e5c5533fd94424 (diff)
downloadgcc-a5f805df5860bd185c77261deaa48049485bfa4e.zip
gcc-a5f805df5860bd185c77261deaa48049485bfa4e.tar.gz
gcc-a5f805df5860bd185c77261deaa48049485bfa4e.tar.bz2
re PR c++/35652 (offset warning should be given in the front-end)
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. From-SVN: r145102
Diffstat (limited to 'gcc/c-common.c')
-rw-r--r--gcc/c-common.c34
1 files changed, 30 insertions, 4 deletions
diff --git a/gcc/c-common.c b/gcc/c-common.c
index cc00511..317c1d7 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 ();