]> git.ozlabs.org Git - ccan/commitdiff
bytestring, crypto/hmac_sha256, darray, mem, tal: avoid mem* with NULL.
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 9 Jan 2023 02:11:22 +0000 (12:41 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 16 Mar 2023 23:22:47 +0000 (09:52 +1030)
I swear the C standards committee likes compilers more than they like
programmers :(

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/bytestring/bytestring.h
ccan/crypto/hmac_sha256/hmac_sha256.c
ccan/darray/darray.h
ccan/mem/mem.h
ccan/tal/tal.c

index bc99e7951c739e94221148e12491ea994642fbae..a0689db15b2f4457a9a15525edf08ee5ae4b55f2 100644 (file)
@@ -203,8 +203,13 @@ static inline const char *bytestring_rindex(struct bytestring haystack,
 static inline struct bytestring bytestring_bytestring(struct bytestring haystack,
                                                      struct bytestring needle)
 {
-       const char *p = memmem(haystack.ptr, haystack.len,
-                              needle.ptr, needle.len);
+       const char *p;
+
+       /* Allow needle.ptr == NULL, without memmem sanitizer complaining */
+       if (needle.len == 0)
+               return bytestring(haystack.ptr, 0);
+
+       p = memmem(haystack.ptr, haystack.len, needle.ptr, needle.len);
        if (p)
                return bytestring(p, needle.len);
        else
index 0392afe5c1127134e61e48d864dffe94252aeaa8..2238f9dc8fffbd4ee049ec8c965e0c11aeef37e2 100644 (file)
@@ -35,7 +35,8 @@ void hmac_sha256_init(struct hmac_sha256_ctx *ctx,
         *  (e.g., if K is of length 20 bytes and B=64, then K will be
         *   appended with 44 zero bytes 0x00)
         */
-       memcpy(k_ipad, k, ksize);
+       if (ksize != 0)
+               memcpy(k_ipad, k, ksize);
        memset((char *)k_ipad + ksize, 0, HMAC_SHA256_BLOCKSIZE - ksize);
 
        /*
index 58470fdee6c5561e49c265b1a975fa90b6b9ca26..0b98fdacddb4febe6d0d92bb5d07bfd4a454fd62 100644 (file)
@@ -183,15 +183,21 @@ typedef darray(unsigned long)  darray_ulong;
 
 #define darray_append_items(arr, items, count) do { \
                size_t count_ = (count), oldSize_ = (arr).size; \
-               darray_resize(arr, oldSize_ + count_); \
-               memcpy((arr).item + oldSize_, items, count_ * sizeof(*(arr).item)); \
+               /* Don't memcpy NULL! */                        \
+               if (count_) {                                   \
+                       darray_resize(arr, oldSize_ + count_);          \
+                       memcpy((arr).item + oldSize_, items, count_ * sizeof(*(arr).item)); \
+               }                                                       \
        } while(0)
 
 #define darray_prepend_items(arr, items, count) do { \
                size_t count_ = (count), oldSize_ = (arr).size; \
                darray_resize(arr, count_ + oldSize_); \
-               memmove((arr).item + count_, (arr).item, oldSize_ * sizeof(*(arr).item)); \
-               memcpy((arr).item, items, count_ * sizeof(*(arr).item)); \
+               /* Don't memcpy NULL! */                        \
+               if (count_) {                                   \
+                       memmove((arr).item + count_, (arr).item, oldSize_ * sizeof(*(arr).item)); \
+                       memcpy((arr).item, items, count_ * sizeof(*(arr).item)); \
+               }                                                       \
        } while(0)
 
 #define darray_append_items_nullterminate(arr, items, count) do { \
index 19f69c038c67efe1e11ac8ab5cec036123aa3b95..20286dcbefd42f99a7e88932ab1ef4cda115f2e2 100644 (file)
@@ -104,7 +104,7 @@ void *memcchr(void const *data, int c, size_t data_len);
 PURE_FUNCTION
 static inline bool memeq(const void *a, size_t al, const void *b, size_t bl)
 {
-       return al == bl && !memcmp(a, b, bl);
+       return al == bl && (al == 0 || !memcmp(a, b, bl));
 }
 
 /**
index 2d05dd93f73b06a35538b1697ccf2eb56cdce2e0..31e8e2f66f691aa3e80f360b8d12c0cbb52fc377 100644 (file)
@@ -803,7 +803,7 @@ void *tal_dup_(const tal_t *ctx, const void *p, size_t size,
        }
 
        ret = tal_alloc_arr_(ctx, size, n + extra, false, label);
-       if (ret)
+       if (ret && p)
                memcpy(ret, p, nbytes);
        return ret;
 }