diff options
author | Andrew Kaster <andrewdkaster@gmail.com> | 2021-05-24 08:00:59 -0600 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-27 15:18:03 +0200 |
commit | 1a0eed705ce5348a9528bb1ecc184e921dcbac2b (patch) | |
tree | 07c9b98d8a218947b6ce1d97e6eb44de906c47d1 /Userland/Services/DHCPClient/DHCPv4.h | |
parent | 723b8586ecb18e004e409240c69d44a521abe21d (diff) | |
download | serenity-1a0eed705ce5348a9528bb1ecc184e921dcbac2b.zip |
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.
Diffstat (limited to 'Userland/Services/DHCPClient/DHCPv4.h')
-rw-r--r-- | Userland/Services/DHCPClient/DHCPv4.h | 6 |
1 files 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<DHCPOption> : public GenericTraits<DHCPOption> { struct ParsedDHCPv4Options { template<typename T> - Optional<const T> get(DHCPOption option_name) const + Optional<const T> get(DHCPOption option_name) const requires(IsTriviallyCopyable<T>) { 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<typename T> |