LibCore: Tolerate misaligned addresses in struct hostent
macOS's C library is not a good neighbor and doesn't ensure that the entry in struct hostent's h_addr_list are aligned properly for a char const*. In Socket::connect, use ByteReader instead of a c-style cast to work around this possible misalignment.
This commit is contained in:
parent
fac4eab415
commit
f7c7954314
Notes:
sideshowbarker
2024-07-18 09:11:11 +09:00
Author: https://github.com/ADKaster Commit: https://github.com/SerenityOS/serenity/commit/f7c79543149 Pull-request: https://github.com/SerenityOS/serenity/pull/8633 Issue: https://github.com/SerenityOS/serenity/issues/7158 Issue: https://github.com/SerenityOS/serenity/issues/8629
1 changed files with 4 additions and 1 deletions
|
@ -5,6 +5,7 @@
|
|||
*/
|
||||
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/ByteReader.h>
|
||||
#include <AK/Debug.h>
|
||||
#include <LibCore/Notifier.h>
|
||||
#include <LibCore/Socket.h>
|
||||
|
@ -58,7 +59,9 @@ bool Socket::connect(const String& hostname, int port)
|
|||
return false;
|
||||
}
|
||||
|
||||
IPv4Address host_address((const u8*)hostent->h_addr_list[0]);
|
||||
// On macOS, the pointer in the hostent structure is misaligned. Load it using ByteReader to avoid UB
|
||||
auto* host_addr = AK::ByteReader::load_pointer<u8 const>(reinterpret_cast<u8 const*>(&hostent->h_addr_list[0]));
|
||||
IPv4Address host_address(host_addr);
|
||||
dbgln_if(CSOCKET_DEBUG, "Socket::connect: Resolved '{}' to {}", hostname, host_address);
|
||||
return connect(host_address, port);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue