[Bug c++/35652] [4.2/4.3/4.4 Regression] offset warning should be given in the front-end
--- Comment #15 from andreasmeier80 at gmx dot de 2009-03-27 07:16 --- Approved here: http://gcc.gnu.org/ml/gcc-patches/2009-03/msg01079.html -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35652
[Bug c++/35652] [4.2/4.3/4.4 Regression] offset warning should be given in the front-end
--- Comment #14 from manu at gcc dot gnu dot org 2009-02-08 15:46 --- Patch: http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00285.html -- manu at gcc dot gnu dot org changed: What|Removed |Added URL||http://gcc.gnu.org/ml/gcc- ||patches/2009- ||02/msg00285.html Keywords||patch Last reconfirmed|2008-03-21 12:49:51 |2009-02-08 15:46:08 date|| http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35652
[Bug c++/35652] [4.2/4.3/4.4 Regression] offset warning should be given in the front-end
--- Comment #13 from bonzini at gnu dot org 2009-02-03 16:40 --- I agree that the patch is correct *without* the -1, so... ping :) -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35652
[Bug c++/35652] [4.2/4.3/4.4 Regression] offset warning should be given in the front-end
--- Comment #9 from rguenther at suse dot de 2008-11-02 12:20 --- Subject: Re: [4.2/4.3/4.4 Regression] offset warning should be given in the front-end On Sat, 1 Nov 2008, manu at gcc dot gnu dot org wrote: --- 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? Keep in mind that one-after the string is ok, so ... @@ -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; ... the -1 is wrong here. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35652
[Bug c++/35652] [4.2/4.3/4.4 Regression] offset warning should be given in the front-end
--- Comment #10 from manu at gcc dot gnu dot org 2008-11-02 12:53 --- (In reply to comment #9) 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? Keep in mind that one-after the string is ok, so ... Do you mean one after the null character? If you have str = abcd. Then sizeof(str) is 5 and str + sizeof(str) points outside the string. (str[4] is the null character). @@ -3322,10 +3323,36 @@ pointer_int_sum (enum tree_code resultco + { + HOST_WIDE_INT max = TREE_STRING_LENGTH (string_cst) - 1; ... the -1 is wrong here. TREE_STRING_LENGTH is the size of the character array, not the string. Are you sure it is wrong? -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35652
[Bug c++/35652] [4.2/4.3/4.4 Regression] offset warning should be given in the front-end
--- Comment #11 from rguenth at gcc dot gnu dot org 2008-11-02 13:02 --- I'm not sure. Does TREE_STRING_LENGTH in the particular case include the NULL character? Does sizeof(str) include the NULL character? In principle it is allowed to have a pointer point one after the last element of an array. That IMHO would include the NULL character, so for foo pointing to foo + 4 would be ok. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35652
Re: [Bug c++/35652] [4.2/4.3/4.4 Regression] offset warning should be given in the front-end
Sent from my iPhone On Nov 2, 2008, at 4:53 AM, manu at gcc dot gnu dot org [EMAIL PROTECTED] wrote: --- Comment #10 from manu at gcc dot gnu dot org 2008-11-02 12:53 --- (In reply to comment #9) 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? Keep in mind that one-after the string is ok, so ... Do you mean one after the null character? If you have str = abcd. Then sizeof(str) is 5 and str + sizeof(str) points outside the string. (str[4] is the null character). That is still well defined. Taking the address of one element past the end of the array is well defined. Just you can not derefence it. Thanks, Andrew Pinski @@ -3322,10 +3323,36 @@ pointer_int_sum (enum tree_code resultco + { + HOST_WIDE_INT max = TREE_STRING_LENGTH (string_cst) - 1; ... the -1 is wrong here. TREE_STRING_LENGTH is the size of the character array, not the string. Are you sure it is wrong? -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35652
[Bug c++/35652] [4.2/4.3/4.4 Regression] offset warning should be given in the front-end
--- Comment #12 from pinskia at gmail dot com 2008-11-02 13:07 --- Subject: Re: [4.2/4.3/4.4 Regression] offset warning should be given in the front-end Sent from my iPhone On Nov 2, 2008, at 4:53 AM, manu at gcc dot gnu dot org [EMAIL PROTECTED] wrote: --- Comment #10 from manu at gcc dot gnu dot org 2008-11-02 12:53 --- (In reply to comment #9) 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? Keep in mind that one-after the string is ok, so ... Do you mean one after the null character? If you have str = abcd. Then sizeof(str) is 5 and str + sizeof(str) points outside the string. (str[4] is the null character). That is still well defined. Taking the address of one element past the end of the array is well defined. Just you can not derefence it. Thanks, Andrew Pinski @@ -3322,10 +3323,36 @@ pointer_int_sum (enum tree_code resultco + { + HOST_WIDE_INT max = TREE_STRING_LENGTH (string_cst) - 1; ... the -1 is wrong here. TREE_STRING_LENGTH is the size of the character array, not the string. Are you sure it is wrong? -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35652 -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35652
[Bug c++/35652] [4.2/4.3/4.4 Regression] offset warning should be given in the front-end
--- 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
[Bug c++/35652] [4.2/4.3/4.4 Regression] offset warning should be given in the front-end
--- Comment #7 from manu at gcc dot gnu dot org 2008-10-30 22:44 --- The location info seems to be fixed in mainline. Nonetheless, I agree completely with Richard. This warning belongs in the front-end. Moreover we fail to diagnose: const char *s = 'x' + y; in both C and C++. Also, this warning does not really requires -O2, so it should work at -O0 too. -- manu at gcc dot gnu dot org changed: What|Removed |Added CC||manu at gcc dot gnu dot org Summary|[4.2/4.3/4.4 Regression]|[4.2/4.3/4.4 Regression] |Location information|offset warning should be |incorrect on string bounds |given in the front-end |warning | http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35652