[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: string-desc: Fix undefined behaviour
From: |
Jeffrey Walton |
Subject: |
Re: string-desc: Fix undefined behaviour |
Date: |
Mon, 9 Sep 2024 22:24:04 -0400 |
On Mon, Sep 9, 2024 at 8:08 PM Bruno Haible <bruno@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 <bruno@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