diff options
author | Andrew Kaster <akaster@serenityos.org> | 2021-07-10 13:00:31 -0600 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2021-07-12 18:42:45 +0430 |
commit | f7c795431498b3f2700e24b8dd7b2ff5b35afd33 (patch) | |
tree | 02b3ae5bc452c7cc8e72bfea43509a29cffca294 | |
parent | fac4eab415c9066679f85699aa0a4c2bd4240bcd (diff) | |
download | serenity-f7c795431498b3f2700e24b8dd7b2ff5b35afd33.zip |
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.
-rw-r--r-- | Userland/Libraries/LibCore/Socket.cpp | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/Userland/Libraries/LibCore/Socket.cpp b/Userland/Libraries/LibCore/Socket.cpp index a21445e84c..6e60bfa8d7 100644 --- a/Userland/Libraries/LibCore/Socket.cpp +++ b/Userland/Libraries/LibCore/Socket.cpp @@ -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); } |