From 8dee9057dba1f92e2b968ee0e52f3630fdc1c979 Mon Sep 17 00:00:00 2001 From: Kershaw Chang Date: Mon, 7 Feb 2022 14:50:24 +0000 Subject: [PATCH] Bug 1753954 - Normalize single number with trailing dot correctly, r=necko-reviewers,valentin Differential Revision: https://phabricator.services.mozilla.com/D138004 --- netwerk/base/nsStandardURL.cpp | 6 ++++++ netwerk/test/gtest/TestStandardURL.cpp | 9 ++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/netwerk/base/nsStandardURL.cpp b/netwerk/base/nsStandardURL.cpp index 816768b226d3..fc70218b048e 100644 --- a/netwerk/base/nsStandardURL.cpp +++ b/netwerk/base/nsStandardURL.cpp @@ -604,6 +604,12 @@ nsresult nsStandardURL::NormalizeIPv4(const nsACString& host, ipv4 += number << (8 * (3 - i)); } + // A special case for ipv4 URL like "127." should have the same result as + // "127". + if (dotCount == 1 && dotIndex[0] == length - 1) { + ipv4 = (ipv4 & 0xff000000) >> 24; + } + uint8_t ipSegments[4]; NetworkEndian::writeUint32(ipSegments, ipv4); result = nsPrintfCString("%d.%d.%d.%d", ipSegments[0], ipSegments[1], diff --git a/netwerk/test/gtest/TestStandardURL.cpp b/netwerk/test/gtest/TestStandardURL.cpp index b79c735e74ca..877539c60776 100644 --- a/netwerk/test/gtest/TestStandardURL.cpp +++ b/netwerk/test/gtest/TestStandardURL.cpp @@ -241,11 +241,18 @@ TEST(TestStandardURL, From_test_standardurldotjs) "1.2.3.4.5", "010000000000000000", "2+3", "0.0.0.-1", "1.2.3.4..", "1..2", - ".1.2.3.4"}; + ".1.2.3.4", ".127"}; for (auto& nonIPv4 : nonIPv4s) { nsCString encHost(nonIPv4); ASSERT_EQ(NS_ERROR_FAILURE, Test_NormalizeIPv4(encHost, result)); } + + const char* oneOrNoDotsIPv4s[] = {"127", "127."}; + for (auto& localIPv4 : oneOrNoDotsIPv4s) { + nsCString encHost(localIPv4); + ASSERT_EQ(NS_OK, Test_NormalizeIPv4(encHost, result)); + ASSERT_TRUE(result.EqualsLiteral("0.0.0.127")); + } } #define TEST_COUNT 10000