From 9e800830de8a9bbb9f103f120ea7de9220b6869d Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 9 Jan 2023 12:41:22 +1030 Subject: [PATCH] bytestring, crypto/hmac_sha256, darray, mem, tal: avoid mem* with NULL. I swear the C standards committee likes compilers more than they like programmers :( Signed-off-by: Rusty Russell --- ccan/bytestring/bytestring.h | 9 +++++++-- ccan/crypto/hmac_sha256/hmac_sha256.c | 3 ++- ccan/darray/darray.h | 14 ++++++++++---- ccan/mem/mem.h | 2 +- ccan/tal/tal.c | 2 +- 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/ccan/bytestring/bytestring.h b/ccan/bytestring/bytestring.h index bc99e795..a0689db1 100644 --- a/ccan/bytestring/bytestring.h +++ b/ccan/bytestring/bytestring.h @@ -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 diff --git a/ccan/crypto/hmac_sha256/hmac_sha256.c b/ccan/crypto/hmac_sha256/hmac_sha256.c index 0392afe5..2238f9dc 100644 --- a/ccan/crypto/hmac_sha256/hmac_sha256.c +++ b/ccan/crypto/hmac_sha256/hmac_sha256.c @@ -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); /* diff --git a/ccan/darray/darray.h b/ccan/darray/darray.h index 58470fde..0b98fdac 100644 --- a/ccan/darray/darray.h +++ b/ccan/darray/darray.h @@ -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 { \ diff --git a/ccan/mem/mem.h b/ccan/mem/mem.h index 19f69c03..20286dcb 100644 --- a/ccan/mem/mem.h +++ b/ccan/mem/mem.h @@ -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)); } /** diff --git a/ccan/tal/tal.c b/ccan/tal/tal.c index 2d05dd93..31e8e2f6 100644 --- a/ccan/tal/tal.c +++ b/ccan/tal/tal.c @@ -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; } -- 2.39.2