mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:27:35 +00:00
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.
This commit is contained in:
parent
723b8586ec
commit
1a0eed705c
1 changed files with 4 additions and 2 deletions
|
@ -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>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue