From 7b80e5b66c9a3de583758ca30eef193ffdea334e Mon Sep 17 00:00:00 2001 From: practicalswift Date: Tue, 2 Jan 2018 14:25:43 +0100 Subject: [PATCH] Avoid undefined behaviour in eq_var(p1, p2, field) memcmp((p1)->field, (p2)->field, ...) results in undefined behaviour if (p1)->field or (p2)->field is NULL. This holds also when tal_count((p1)->field) * sizeof(*(p1)->field) == 0. --- wire/test/run-peer-wire.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/wire/test/run-peer-wire.c b/wire/test/run-peer-wire.c index db96ddf8b..c07c34b74 100644 --- a/wire/test/run-peer-wire.c +++ b/wire/test/run-peer-wire.c @@ -68,7 +68,8 @@ static void set_pubkey(struct pubkey *key) sizeof((p1)->field)) == 0) #define eq_var(p1, p2, field) \ - (tal_count((p1)->field) == tal_count((p2)->field) && memcmp((p1)->field, (p2)->field, tal_count((p1)->field) * sizeof(*(p1)->field)) == 0) + (tal_count((p1)->field) == tal_count((p2)->field) \ + && (tal_count((p1)->field) == 0 || memcmp((p1)->field, (p2)->field, tal_len((p1)->field)) == 0)) /* Convenience structs for everyone! */ struct msg_error {