This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Exploiting knowing sizes of string.
- From: Joseph Myers <joseph at codesourcery dot com>
- To: Richard Earnshaw <Richard dot Earnshaw at foss dot arm dot com>
- Cc: Ondřej Bílka <neleai at seznam dot cz>, Jakub Jelinek <jakub at redhat dot com>, <gcc at gcc dot gnu dot org>
- Date: Thu, 4 Jun 2015 16:01:50 +0000
- Subject: Re: Exploiting knowing sizes of string.
- Authentication-results: sourceware.org; auth=none
- References: <20150604105929 dot GA19141 at domone> <alpine dot DEB dot 2 dot 10 dot 1506041206200 dot 31862 at digraph dot polyomino dot org dot uk> <20150604123319 dot GF10247 at tucnak dot redhat dot com> <20150604125331 dot GA22076 at domone> <20150604125920 dot GG10247 at tucnak dot redhat dot com> <20150604142304 dot GA23875 at domone> <5570757D dot 8050809 at foss dot arm dot com>
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.
--
Joseph S. Myers
joseph@codesourcery.com