You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by Ruediger Pluem <rp...@apache.org> on 2021/10/25 11:30:05 UTC

Re: svn commit: r1894551 - in /apr/apr/trunk: buckets/apr_brigade.c configure.in include/apr.h.in include/apr.hnw include/apr.hw include/apr.hwc


On 10/25/21 12:31 PM, minfrin@apache.org wrote:
> Author: minfrin
> Date: Mon Oct 25 10:31:32 2021
> New Revision: 1894551
> 
> URL: http://svn.apache.org/viewvc?rev=1894551&view=rev
> Log:
> apr_brigade_split_boundary: Provide a memmem implementation on platforms that
> do not have one.
> 
> Modified:
>     apr/apr/trunk/buckets/apr_brigade.c
>     apr/apr/trunk/configure.in
>     apr/apr/trunk/include/apr.h.in
>     apr/apr/trunk/include/apr.hnw
>     apr/apr/trunk/include/apr.hw
>     apr/apr/trunk/include/apr.hwc
> 
> Modified: apr/apr/trunk/buckets/apr_brigade.c
> URL: http://svn.apache.org/viewvc/apr/apr/trunk/buckets/apr_brigade.c?rev=1894551&r1=1894550&r2=1894551&view=diff
> ==============================================================================
> --- apr/apr/trunk/buckets/apr_brigade.c (original)
> +++ apr/apr/trunk/buckets/apr_brigade.c Mon Oct 25 10:31:32 2021
> @@ -387,6 +387,36 @@ APR_DECLARE(apr_status_t) apr_brigade_sp
>      return APR_SUCCESS;
>  }
>  
> +#if !APR_HAVE_MEMMEM
> +static const void *
> +memmem(const void *hay, size_t hay_len, const void *needle, size_t needle_len)
> +{
> +
> +    if (hay_len < needle_len || !needle_len || !hay_len) {
> +        return NULL;
> +    }
> +    else {
> +
> +        apr_size_t len = hay_len - needle_len + 1;
> +        const void *end = hay + hay_len;
> +        const void *begin = hay;

begin is unused.

> +
> +        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.

> +            len = end - hay - needle_len + 1;
> +
> +            if (memcmp(hay, needle, needle_len) == 0 ) {
> +                break;
> +            }
> +
> +            --len;
> +            ++hay;
> +        }
> +
> +        return hay;
> +    }
> +}
> +#endif
> +
>  APR_DECLARE(apr_status_t) apr_brigade_split_boundary(apr_bucket_brigade *bbOut,
>                                                       apr_bucket_brigade *bbIn,
>                                                       apr_read_type_e block,
> 

Regards

Rüdiger


Re: svn commit: r1894551 - in /apr/apr/trunk: buckets/apr_brigade.c configure.in include/apr.h.in include/apr.hnw include/apr.hw include/apr.hwc

Posted by Ruediger Pluem <rp...@apache.org>.

On 10/25/21 2:27 PM, Graham Leggett wrote:
> On 25 Oct 2021, at 12:30, Ruediger Pluem <rp...@apache.org> 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


Re: svn commit: r1894551 - in /apr/apr/trunk: buckets/apr_brigade.c configure.in include/apr.h.in include/apr.hnw include/apr.hw include/apr.hwc

Posted by Graham Leggett <mi...@sharp.fm>.
On 25 Oct 2021, at 12:30, Ruediger Pluem <rp...@apache.org> 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.

Regards,
Graham
—