mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:57:34 +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 {
|
struct ParsedDHCPv4Options {
|
||||||
template<typename T>
|
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);
|
auto option = options.get(option_name);
|
||||||
if (!option.has_value()) {
|
if (!option.has_value()) {
|
||||||
|
@ -123,7 +123,9 @@ struct ParsedDHCPv4Options {
|
||||||
auto& value = option.value();
|
auto& value = option.value();
|
||||||
if (value.length != sizeof(T))
|
if (value.length != sizeof(T))
|
||||||
return {};
|
return {};
|
||||||
return *(const T*)value.value;
|
T t;
|
||||||
|
__builtin_memcpy(&t, value.value, value.length);
|
||||||
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue