This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: PR c++/35652 [4.2/4.3/4.4 Regression] offset warning should be given in the front-end
- From: Rafael Espindola <espindola at google dot com>
- To: Manuel LÃpez-IbÃÃez <lopezibanez at gmail dot com>
- Cc: Gcc Patch List <gcc-patches at gcc dot gnu dot org>, Mark Mitchell <mark at codesourcery dot com>, Jason Merrill <jason at redhat dot com>, Nathan Sidwell <nathan at codesourcery dot com>
- Date: Mon, 30 Mar 2009 11:56:03 +0100
- Subject: Re: PR c++/35652 [4.2/4.3/4.4 Regression] offset warning should be given in the front-end
- References: <6c33472e0811030736i272ea3a9r996e4b5a5fd3b3c7@mail.gmail.com>
With this patch I now get a warning for
------------------------------------------
#include <string.h>
int f(char *s)
{
return strcmp (s, "");
}
------------------------------------
test.c: In function 'f':
test.c:5: error: offset '2' outside bounds of constant string
test.c:5: error: offset '3' outside bounds of constant string
The problem is that strcmp was expanded into something equivalent to
size_t __s2_len = 0;
__const unsigned char *__s1 = (__const unsigned char *) (__const char *) (s);
register int __result = __s1[0] - ((__const unsigned char *)
(__const char *) (""))[0];
if (__s2_len > 0 && __result == 0)
{
__result = (__s1[1] - ((__const unsigned char *) (__const char
*) (""))[1]);
if (__s2_len > 1 && __result == 0)
{
__result = (__s1[2] - ((__const unsigned char *) (__const char *) (""))[2]);
if (__s2_len > 2 && __result == 0) __result = (__s1[3] - ((__const
unsigned char *) (__const char *) (""))[3]);
}
}
return __result;
The code is safe, since the access to ""[2] is protected by "if
(__s2_len > 1)" for example.
Cheers,
--
Rafael Avila de Espindola
Google | Gordon House | Barrow Street | Dublin 4 | Ireland
Registered in Dublin, Ireland | Registration Number: 368047