From f7c795431498b3f2700e24b8dd7b2ff5b35afd33 Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Sat, 10 Jul 2021 13:00:31 -0600 Subject: [PATCH] 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. --- Userland/Libraries/LibCore/Socket.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibCore/Socket.cpp b/Userland/Libraries/LibCore/Socket.cpp index a21445e84c5..6e60bfa8d7a 100644 --- a/Userland/Libraries/LibCore/Socket.cpp +++ b/Userland/Libraries/LibCore/Socket.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include #include @@ -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(reinterpret_cast(&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); }