From f8c2beec7cf30d10bf3970ff678c962949bc996a Mon Sep 17 00:00:00 2001 From: Georgiy Komarov Date: Sat, 17 Apr 2021 21:49:22 +0300 Subject: [PATCH] DHCPClient: Fix undefined behaviour when calling memcpy() (#6416) Calling memcpy with null pointers results in undefined behaviour, even if count is zero. This in turns is exploited by GCC. For example, the following code: memcpy (dst, src, n); if (!src) return; src[0] = 0xcafe; will be optimized as: memcpy (dst, src, n); src[0] = 0xcafe; IOW the test for NULL is gone. --- Userland/Services/DHCPClient/DHCPv4.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Userland/Services/DHCPClient/DHCPv4.h b/Userland/Services/DHCPClient/DHCPv4.h index 530ac7e758..82633d4ee6 100644 --- a/Userland/Services/DHCPClient/DHCPv4.h +++ b/Userland/Services/DHCPClient/DHCPv4.h @@ -277,7 +277,8 @@ public: options[next_option_offset++] = (u8)option; memcpy(options + next_option_offset, &length, 1); next_option_offset++; - memcpy(options + next_option_offset, data, length); + if (data && length) + memcpy(options + next_option_offset, data, length); next_option_offset += length; }