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:
Andrew Kaster 2021-07-10 13:00:31 -06:00 committed by Ali Mohammad Pur
parent fac4eab415
commit f7c7954314
Notes: sideshowbarker 2024-07-18 09:11:11 +09:00

View File

@ -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);
}