From e497b9bd88db2302ddc32317bd600c8176c579db Mon Sep 17 00:00:00 2001 From: Richard Guenther Date: Thu, 19 May 2011 10:45:26 +0000 Subject: [PATCH] re PR c/48985 (bogus buffer overflow warning and abort on static flexible array member) 2011-05-19 Richard Guenther PR middle-end/48985 * tree-object-size.c (addr_object_size): If the pointed-to variable is a decl use DECL_SIZE_UNIT instead of TYPE_SIZE_UNIT. * gcc.dg/builtin-object-size-11.c: New testcase. From-SVN: r173901 --- gcc/ChangeLog | 6 ++++++ gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gcc.dg/builtin-object-size-11.c | 20 +++++++++++++++++++ gcc/tree-object-size.c | 6 ++++++ 4 files changed, 37 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/builtin-object-size-11.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index a653ae8442f..b03cc9bc4af 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2011-05-19 Richard Guenther + + PR middle-end/48985 + * tree-object-size.c (addr_object_size): If the pointed-to + variable is a decl use DECL_SIZE_UNIT instead of TYPE_SIZE_UNIT. + 2011-05-19 Richard Guenther * gimple.c (gimple_types_compatible_p_1): Compare names of diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index f9defe50594..242a33b0283 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2011-05-19 Richard Guenther + + PR middle-end/48985 + * gcc.dg/builtin-object-size-11.c: New testcase. + 2011-05-19 Tom de Vries PR target/45098 diff --git a/gcc/testsuite/gcc.dg/builtin-object-size-11.c b/gcc/testsuite/gcc.dg/builtin-object-size-11.c new file mode 100644 index 00000000000..427b94d4335 --- /dev/null +++ b/gcc/testsuite/gcc.dg/builtin-object-size-11.c @@ -0,0 +1,20 @@ +/* PR48985 */ +/* { dg-do run } */ + +extern void abort (void); + +struct s { + int i; + char c[]; +} s = { 1, "01234" }; + +__SIZE_TYPE__ f (void) { return __builtin_object_size (&s.c, 0); } + +int +main() +{ + if (f() != sizeof ("01234")) + abort (); + + return 0; +} diff --git a/gcc/tree-object-size.c b/gcc/tree-object-size.c index 043b445bf2e..41118d25b2e 100644 --- a/gcc/tree-object-size.c +++ b/gcc/tree-object-size.c @@ -204,6 +204,12 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, if (sz != unknown[object_size_type] && sz < offset_limit) pt_var_size = size_int (sz); } + else if (pt_var + && DECL_P (pt_var) + && host_integerp (DECL_SIZE_UNIT (pt_var), 1) + && (unsigned HOST_WIDE_INT) + tree_low_cst (DECL_SIZE_UNIT (pt_var), 1) < offset_limit) + pt_var_size = DECL_SIZE_UNIT (pt_var); else if (pt_var && (SSA_VAR_P (pt_var) || TREE_CODE (pt_var) == STRING_CST) && TYPE_SIZE_UNIT (TREE_TYPE (pt_var)) -- 2.30.2