From 32fea147eaac4b811440a29d362fdad7e2c5a4ec Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Sun, 16 Nov 2014 10:14:54 -0800 Subject: [PATCH] Add getaddrinfo(3) tests for NULL arguments. According to https://github.com/ukanth/afwall/pull/213 some OEMs have shipped a getaddrinfo(3) that crashes given NULL hostnames. Change-Id: I9cea5fdd68546b7c64cf47e10e2b2b4d672b69d0 --- tests/netdb_test.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/netdb_test.cpp b/tests/netdb_test.cpp index ef2c8dac2..0cebe4e8e 100644 --- a/tests/netdb_test.cpp +++ b/tests/netdb_test.cpp @@ -21,6 +21,23 @@ #include #include +TEST(netdb, getaddrinfo_NULL_host) { + // It's okay for the host argument to be NULL, as long as service isn't. + addrinfo* ai = NULL; + ASSERT_EQ(0, getaddrinfo(NULL, "smtp", NULL, &ai)); + // (sockaddr_in::sin_port and sockaddr_in6::sin6_port overlap.) + ASSERT_EQ(25U, ntohs(reinterpret_cast(ai->ai_addr)->sin_port)); + freeaddrinfo(ai); +} + +TEST(netdb, getaddrinfo_NULL_service) { + // It's okay for the service argument to be NULL, as long as host isn't. + addrinfo* ai = NULL; + ASSERT_EQ(0, getaddrinfo("localhost", NULL, NULL, &ai)); + ASSERT_TRUE(ai != NULL); + freeaddrinfo(ai); +} + TEST(netdb, getaddrinfo_NULL_hints) { addrinfo* ai = NULL; ASSERT_EQ(0, getaddrinfo("localhost", "9999", NULL, &ai)); -- 2.11.0