From 75a0d4756fc24c8f5f2c0cc91ff983e8a2dfc2c3 Mon Sep 17 00:00:00 2001 From: XadillaX Date: Sun, 30 May 2021 13:56:08 +0800 Subject: [PATCH] f --- src/node_url.cc | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/node_url.cc b/src/node_url.cc index 08a0c61a97faac..81e315a14dfa5a 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -48,6 +48,9 @@ constexpr char kEOL = -1; // Used in ToUSVString(). constexpr char16_t kUnicodeReplacementCharacter = 0xFFFD; +#define NS_IN6ADDRSZ 16 +#define NS_INT16SZ 2 + // https://url.spec.whatwg.org/#concept-host class URLHost { public: @@ -78,7 +81,7 @@ class URLHost { union Value { std::string domain_or_opaque; uint32_t ipv4; - uint16_t ipv6[8]; + uint16_t ipv6[NS_IN6ADDRSZ / NS_INT16SZ]; ~Value() {} Value() : ipv4(0) {} @@ -801,20 +804,20 @@ void URLHost::ParseIPv6Host(const char* input, size_t length) { CHECK_EQ(type_, HostType::H_FAILED); unsigned char buf[sizeof(struct in6_addr)]; + MaybeStackBuffer ipv6(length + 1); + *(*ipv6 + length) = 0; memset(buf, 0, sizeof(buf)); + memcpy(*ipv6, input, sizeof(const char) * length); - char* zero = const_cast(input + length); - char origin_zero_char = *zero; - *zero = 0; - int ret = inet_pton(AF_INET6, input, buf); - *zero = origin_zero_char; + int ret = uv_inet_pton(AF_INET6, *ipv6, buf); - if (ret <= 0) { + if (ret != 0) { return; } - for (int i = 0; i < 16; i += 2) { - value_.ipv6[i / 2] = (buf[i] << 8) | buf[i + 1]; + // Ref: https://sourceware.org/git/?p=glibc.git;a=blob;f=resolv/inet_ntop.c;h=c4d38c0f951013e51a4fc6eaa8a9b82e146abe5a;hb=HEAD#l119 + for (int i = 0; i < NS_IN6ADDRSZ; i += 2) { + value_.ipv6[i >> 1] = (buf[i] << 8) | buf[i + 1]; } type_ = HostType::H_IPV6;