From 2005ca436ea3089a48a4f1d9182ea09ccd556726 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sat, 1 Apr 2023 14:10:23 +1030 Subject: [PATCH] common/gossmap: don't memcpy NULL, 0, and don't add 0 to NULL pointer. Of course, NULL and length 0 are natural partners, but We Can't Have Nice Things. Signed-off-by: Rusty Russell --- common/gossmap.c | 4 +++- wire/fromwire.c | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/common/gossmap.c b/common/gossmap.c index db5c086e1..a1f70eda8 100644 --- a/common/gossmap.c +++ b/common/gossmap.c @@ -808,7 +808,9 @@ bool gossmap_local_addchan(struct gossmap_localmods *localmods, be16 = cpu_to_be16(tal_bytelen(features)); memcpy(localmods->local + off, &be16, sizeof(be16)); off += sizeof(be16); - memcpy(localmods->local + off, features, tal_bytelen(features)); + /* Damn you, C committee! */ + if (features) + memcpy(localmods->local + off, features, tal_bytelen(features)); off += tal_bytelen(features); /* Skip chain_hash */ diff --git a/wire/fromwire.c b/wire/fromwire.c index 69139ca3b..37727f4a7 100644 --- a/wire/fromwire.c +++ b/wire/fromwire.c @@ -32,9 +32,11 @@ const u8 *fromwire(const u8 **cursor, size_t *max, void *copy, size_t n) SUPERVERBOSE("less than encoding length"); return fromwire_fail(cursor, max); } - *cursor += n; + /* ubsan: runtime error: applying zero offset to null pointer */ + if (*cursor) + *cursor += n; *max -= n; - if (copy) + if (copy && n) memcpy(copy, p, n); return memcheck(p, n); }