[vlc-devel] [vlc-commits] compat: fix strnstr

Rémi Denis-Courmont remi at remlab.net
Thu Oct 8 10:01:24 CEST 2015


Le 2015-10-07 20:44, git at videolan.org a écrit :
> vlc | branch: master | Francois Cartegnie <fcvlcdev at free.fr> | Wed
> Oct  7 19:38:25 2015 +0200| 
> [db41ce0a1692b5894ca93122932c6e3f7cb718b6]
> | committer: Francois Cartegnie
>
> compat: fix strnstr
>
> need to wake up sometimes :/
>
>> 
>> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=db41ce0a1692b5894ca93122932c6e3f7cb718b6
> ---
>
>  compat/strnstr.c |   10 +++++++---
>  1 file changed, 7 insertions(+), 3 deletions(-)
>
> diff --git a/compat/strnstr.c b/compat/strnstr.c
> index badf7d8..3e1f6d5 100644
> --- a/compat/strnstr.c
> +++ b/compat/strnstr.c
> @@ -26,18 +26,22 @@
>
>  char * strnstr (const char *haystack, const char *needle, size_t 
> len)
>  {
> +    if(!needle || !*needle)
> +        return (char*)haystack;

needle == NULL is not defined by FreeBSD documentation (at least). It 
will probably crash on systems that actually provide the function 
natively.

And the empty string case should not need any special handling.

> +
>      const size_t i = strlen(needle);
> -    if( i < len )
> +    if( len < i )
>        return NULL;
>
>      size_t count = len - i;
>
> -    while(count)
> +    do
>      {
>        if( memcmp(haystack, needle, i) )
>          return (char*) haystack;
> -      count--;
>        haystack++;
>      }
> +    while(count--);
> +
>      return NULL;
>  }
>
> _______________________________________________
> vlc-commits mailing list
> vlc-commits at videolan.org
> https://mailman.videolan.org/listinfo/vlc-commits

-- 
Rémi Denis-Courmont
http://www.remlab.net/


More information about the vlc-devel mailing list