bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

string-desc: Fix undefined behaviour


From: Bruno Haible
Subject: string-desc: Fix undefined behaviour
Date: Tue, 10 Sep 2024 02:08:19 +0200

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)






reply via email to

[Prev in Thread] Current Thread [Next in Thread]