Use the NetBSD inet_ntop until the OpenBSD bug is fixed.
Stupidly I found this bug by accident when writing the existing
tests, but I didn't think any real code would hit it. It turns
out that libcore always uses an INET6_ADDRSTRLEN-sized buffer
even when working with AF_INET addresses.
Change-Id: Ieffc8e4bbe9b66b49b033e3e7101c896e097e6f8
diff --git a/tests/arpa_inet_test.cpp b/tests/arpa_inet_test.cpp
index cee9f36..5e53337 100644
--- a/tests/arpa_inet_test.cpp
+++ b/tests/arpa_inet_test.cpp
@@ -59,3 +59,23 @@
char s[INET_ADDRSTRLEN];
ASSERT_STREQ("127.0.0.1", inet_ntop(AF_INET, &ss, s, INET_ADDRSTRLEN));
}
+
+TEST(arpa_inet, inet_ntop_overflow) {
+ // OpenBSD's inet_ntop had a bug where passing a 'size' larger than INET_ADDRSTRLEN
+ // for AF_INET or INET6_ADDRSTRLEN for AF_INET6 would cause inet_ntop to overflow an
+ // internal buffer.
+
+ sockaddr_storage ss4;
+ ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", &ss4));
+
+ sockaddr_storage ss6;
+ ASSERT_EQ(1, inet_pton(AF_INET6, "::1", &ss6));
+
+ char s4[INET_ADDRSTRLEN];
+ char s6[INET6_ADDRSTRLEN];
+ ASSERT_STREQ("127.0.0.1", inet_ntop(AF_INET, &ss4, s4, INET_ADDRSTRLEN));
+ ASSERT_STREQ("127.0.0.1", inet_ntop(AF_INET, &ss4, s4, 2*INET_ADDRSTRLEN));
+ ASSERT_STREQ("::1", inet_ntop(AF_INET6, &ss6, s6, INET_ADDRSTRLEN));
+ ASSERT_STREQ("::1", inet_ntop(AF_INET6, &ss6, s6, INET6_ADDRSTRLEN));
+ ASSERT_STREQ("::1", inet_ntop(AF_INET6, &ss6, s6, 2*INET6_ADDRSTRLEN));
+}