This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc 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: [PATCH v2] Improve memmem.


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;
        }
    }

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