On 10/25/21 2:27 PM, Graham Leggett wrote:
> On 25 Oct 2021, at 12:30, Ruediger Pluem <[email protected]> wrote:
> 
>> begin is unused.
> 
> Will fix.
> 
>>> +
>>> +        while ((hay = memchr(hay, *(char *)needle, len))) {
>>
>> Does memchr have a defined behaviour for len == 0? This can happen if
>> *(char *)needle is found at hay[len -1 ] but the memcmp below is not zero.
>> In this case len becomes 1 below and 0 after the --len.
> 
> Code is a simplified version of this:
> 
> https://github.com/apache/apreq/blob/trunk/library/util.c#L92
> 
> I understand this to be the case.
> 
> The BSD man page on MacOS says “Zero-length strings are always identical” and 
> the function is C90.

The comment above is from the memcmp man page. I was talking about memchr. At 
least for MacOS it says:

 The memchr() function returns a pointer to the byte located, or NULL if
     no such byte exists within n bytes.

This could be interpreted in a way that with n == 0 this is not possible and 
hence NULL is returned. This would do the correct
thing as it leaves the while loop and returns NULL. The Linux man page text is 
a little different. Hence I was not sure if we have
a defined behavior in this case, but probably yes.

Regards

Rüdiger

Reply via email to