Ad PR libc/1730: glibc bug in memmem()

Greg Hudson ghudson@MIT.EDU
Wed May 17 08:41:00 GMT 2000


> You must be a mathematician :-).  Looking for a needle that's bigger
> than the haystack is definitely unphysical.

> Anyway, I'm not sure there is a bug here.  The memmem() function is
> a GNU extension.  We might very well say that haystack_len <
> needle_len invokes undefined behaviour.

Well, sure, you could say that.  But it would be decidedly
inconvenient, and would lead to more error-prone programs.  If I've
read some data from the network and I want to search for a string in
it, it would be poor to have to do a length check before calling
memmem().  Or, if I'm scanning through a haystack looking for multiple
occurrances of a given needle, I shouldn't have to manually check if
I've gotten too close to the end of the string to make a valid call to
memmem().

And there's also an argument by analogy; strstr() works just fine when
the needle is longer than the haystack.  And memchr() works fine if
it's searching through a zero-length data area.  In general, C
functions do not have undefined behavior simply because the answer is
obvious from an inspection of the arguments.


More information about the Libc-alpha mailing list