Commit 6b4656a5 authored by bradnelson@google.com's avatar bradnelson@google.com

Handle h_addr in a more libc independent way.

Which glibc defines h_addr as a macro to h_addr_list[0],
newlib has a separate h_addr member in struct hostent.

To correctly handle this, h_addr should be set to match h_addr_list[0].

BUG=None
R=binji@chromium.org
TEST=bots

Review URL: https://codereview.chromium.org/275533002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@270026 0039d316-1c4b-4281-b951-d872f2087c98
parent 08230555
......@@ -196,6 +196,13 @@ struct hostent* HostResolver::gethostbyname(const char* name) {
}
freeaddrinfo(ai);
#if !defined(h_addr)
// Copy element zero of h_addr_list to h_addr when h_addr is not defined
// as in some libc's h_addr may be a separate member instead of a macro.
hostent_.h_addr = hostent_.h_addr_list[0];
#endif
return &hostent_;
}
......@@ -416,6 +423,10 @@ void HostResolver::hostent_cleanup() {
hostent_.h_name = NULL;
hostent_.h_aliases = NULL;
hostent_.h_addr_list = NULL;
#if !defined(h_addr)
// Initialize h_addr separately in the case where it is not a macro.
hostent_.h_addr = NULL;
#endif
}
} // namespace nacl_io
......
......@@ -321,8 +321,12 @@ TEST_F(FakeHostResolverTest, Gethostbyname) {
in_addr_t** addr_list = reinterpret_cast<in_addr_t**>(host->h_addr_list);
ASSERT_NE(reinterpret_cast<in_addr_t**>(NULL), addr_list);
ASSERT_EQ(NULL, addr_list[1]);
in_addr_t exptected_addr = htonl(FAKE_IP);
ASSERT_EQ(exptected_addr, *addr_list[0]);
in_addr_t expected_addr = htonl(FAKE_IP);
ASSERT_EQ(expected_addr, *addr_list[0]);
// Check that h_addr also matches as in some libc's it may be a separate
// member.
in_addr_t* first_addr = reinterpret_cast<in_addr_t*>(host->h_addr);
ASSERT_EQ(expected_addr, *first_addr);
}
TEST_F(FakeHostResolverTest, Gethostbyname_Failure) {
......@@ -346,6 +350,10 @@ TEST_F(HostResolverTest, Gethostbyname_Numeric) {
ASSERT_NE(reinterpret_cast<in_addr_t**>(NULL), addr_list);
ASSERT_EQ(NULL, addr_list[1]);
ASSERT_EQ(inet_addr("8.8.8.8"), *addr_list[0]);
// Check that h_addr also matches as in some libc's it may be a separate
// member.
in_addr_t* first_addr = reinterpret_cast<in_addr_t*>(host->h_addr);
ASSERT_EQ(inet_addr("8.8.8.8"), *first_addr);
}
// These utility functions are only used for newlib (glibc provides its own
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment