This is the mail archive of the gcc@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: Exploiting knowing sizes of string.


On Thu, Jun 04, 2015 at 04:01:50PM +0000, Joseph Myers wrote:
> On Thu, 4 Jun 2015, Richard Earnshaw wrote:
> 
> > > Change that into
> > > 
> > > int foo(char *s)
> > > {
> > >   int l = strlen (s);
> > >   char *p = memchr (s, 'a', l);
> > >   return p+l;
> > > }
> > > 
> > 
> > Which is still meaningless if 'a' does not appear in s => when the
> > result is NULL + l.
> > 
> > In fact, unless 'a' is the first character the result is possibly
> > meaningless anyway, since you can't know that p+l doesn't point more
> > than one beyond the end of the object.
> > 
> > Perhaps you just meant to return 'p'?
> 
> And if size_t is wider than int, this function truncates the length of the 
> string, so still isn't particularly sensible even if returning p.
>
Correct but doesn't matter that it was mean just to check
transformation, not do anything useful. You could as well use p < s + l - 3.
It just keeps strlen and strchr from being dead, nothing else.

And Joseph you shouldn't restrict yourself only to values that are
present in variables to cover case where its implicit one from strcpy
converted to stpcpy.


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