This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: PR c++/35652 [4.2/4.3/4.4 Regression] offset warning should be given in the front-end


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]