This is the mail archive of the
libc-alpha@sourceware.cygnus.com
mailing list for the glibc project.
Re: Ad PR libc/1730: glibc bug in memmem()
- To: Mark Kettenis <kettenis at wins dot uva dot nl>
- Subject: Re: Ad PR libc/1730: glibc bug in memmem()
- From: Greg Hudson <ghudson at MIT dot EDU>
- Date: Wed, 17 May 2000 11:41:11 -0400
- cc: aj at suse dot de, libc-alpha at sourceware dot cygnus dot com
> 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.