aboutsummaryrefslogtreecommitdiff
path: root/gcc/c-common.c
diff options
context:
space:
mode:
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 ();