This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH v2] Improve memmem.
- From: Paul Eggert <eggert at cs dot ucla dot edu>
- To: OndÅej BÃlka <neleai at seznam dot cz>
- Cc: libc-alpha at sourceware dot org
- Date: Wed, 13 May 2015 18:06:58 -0700
- Subject: Re: [PATCH v2] Improve memmem.
- Authentication-results: sourceware.org; auth=none
- References: <20150513000329 dot GA23595 at domone> <55537C4A dot 20001 at cs dot ucla dot edu> <20150513185107 dot GA4100 at domone>
OndÅej BÃlka wrote:
You wont get illegal acces.
I don't see why not. If memchr returns haystack_end - 1, haystack[1] will be
equivalent to *haystack_end, i.e., one past the end of the haystack.
Here is equivalent without goto. It uses implementation behaviour that
memcmp(x,y,0) doesn't touch anything.
Better, but I think it still has the problem. Also, come to think of it, it's
not clear that we should bother with the 'needle_len == 2' test (are needles of
length 2 really that common?), and things are simpler and smaller without it, so
how about the attached instead?
if (needle_len == 1)
return memchr (haystack, needle[0], haystack_end - haystack);
for (; ;haystack++)
{
haystack = memchr (haystack, needle[0],
haystack_end - needle_len + 1 - haystack);
if (!haystack)
return NULL;
if (haystack[1] == needle[1])
{
if (!memcmp (haystack + 2, needle + 2, needle_len - 2))
return (void *) haystack;
break;
}
}