On Mon, Sep 9, 2024 at 8:08 PM Bruno Haible <br...@clisp.org> wrote:
>
> Running some gnulib tests with clang's UBSAN, I get this error in the file
> test-string-desc.sh.log:
>
>   string-desc-contains.c:39:13: runtime error: null pointer passed as 
> argument 1, which is declared to never be null
>   /usr/include/string.h:391:33: note: nonnull attribute specified here
>   SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior 
> string-desc-contains.c:39:13
>   PASS test-string-desc.sh (exit status: 0)
>
> This patch fixes it.
>
>
> 2024-09-09  Bruno Haible  <br...@clisp.org>
>
>         string-desc: Fix undefined behaviour.
>         * lib/string-desc-contains.c (string_desc_contains): Handle the case 
> of
>         an empty haystack before invoking 'memmem'.
>
> diff --git a/lib/string-desc-contains.c b/lib/string-desc-contains.c
> index 21c52069f6..ec79d6acf0 100644
> --- a/lib/string-desc-contains.c
> +++ b/lib/string-desc-contains.c
> @@ -35,6 +35,8 @@ string_desc_contains (string_desc_t haystack, string_desc_t 
> needle)
>  {
>    if (needle._nbytes == 0)
>      return 0;
> +  if (haystack._nbytes == 0)
> +    return -1;
>    void *found =
>      memmem (haystack._data, haystack._nbytes, needle._data, needle._nbytes);
>    if (found != NULL)

Clang is complaining haystack._data is NULL, not needle._nbytes is 0.

Jeff

Reply via email to