From 1a0eed705ce5348a9528bb1ecc184e921dcbac2b Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Mon, 24 May 2021 08:00:59 -0600 Subject: [PATCH] DHCPClient: Avoid unaligned access when parsing options Just casting a void* to a T* and dereferencing it is not particularly safe. Also UBSAN was complaining. Use memcpy into a default constructed T instead and require that the T be trivially copyable. --- Userland/Services/DHCPClient/DHCPv4.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Userland/Services/DHCPClient/DHCPv4.h b/Userland/Services/DHCPClient/DHCPv4.h index b3165633b4..7e87f84250 100644 --- a/Userland/Services/DHCPClient/DHCPv4.h +++ b/Userland/Services/DHCPClient/DHCPv4.h @@ -114,7 +114,7 @@ struct AK::Traits : public GenericTraits { struct ParsedDHCPv4Options { template - Optional get(DHCPOption option_name) const + Optional get(DHCPOption option_name) const requires(IsTriviallyCopyable) { auto option = options.get(option_name); if (!option.has_value()) { @@ -123,7 +123,9 @@ struct ParsedDHCPv4Options { auto& value = option.value(); if (value.length != sizeof(T)) return {}; - return *(const T*)value.value; + T t; + __builtin_memcpy(&t, value.value, value.length); + return t; } template