Bug 1301344 - Bail out from canonicalizing IPv4 first. r=valentin
authorJunior Hsu <juhsu@mozilla.com>
Thu, 08 Sep 2016 02:59:00 -0400
changeset 354666 ab059611db16cfeee8cbc1cdf88c8c371c2ae59f
parent 354665 540bd4b5aa3dda58a032982b1bab1d0c0eff783a
child 354667 bb033983fe8d5128bc3e01df3b56974898e25c57
push id6570
push userraliiev@mozilla.com
push dateMon, 14 Nov 2016 12:26:13 +0000
treeherdermozilla-beta@f455459b2ae5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersvalentin
bugs1301344
milestone51.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1301344 - Bail out from canonicalizing IPv4 first. r=valentin
netwerk/base/nsStandardURL.cpp
--- a/netwerk/base/nsStandardURL.cpp
+++ b/netwerk/base/nsStandardURL.cpp
@@ -397,17 +397,17 @@ nsStandardURL::IsValidOfBase(unsigned ch
     } else if (c == '8' || c== '9') {
         return base != 8;
     } else if (('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')) {
         return base == 16;
     }
     return false;
 }
 
-/* static */ nsresult
+/* static */ inline nsresult
 nsStandardURL::ParseIPv4Number(nsCString &input, uint32_t &number)
 {
     if (input.Length() == 0) {
         return NS_ERROR_FAILURE;
     }
     uint32_t base;
     uint32_t prefixLength = 0;
 
@@ -467,24 +467,26 @@ nsStandardURL::ParseIPv4Number(nsCString
     }
     return NS_ERROR_FAILURE;
 }
 
 // IPv4 parser spec: https://url.spec.whatwg.org/#concept-ipv4-parser
 /* static */ nsresult
 nsStandardURL::NormalizeIPv4(const nsCSubstring &host, nsCString &result)
 {
-    if (FindInReadable(NS_LITERAL_CSTRING(".."), host)) {
+    if (host.Length() == 0 ||
+        host[0] < '0' || '9' < host[0] || // bail-out fast
+        FindInReadable(NS_LITERAL_CSTRING(".."), host)) {
         return NS_ERROR_FAILURE;
     }
+
     nsTArray<nsCString> parts;
     if (!ParseString(host, '.', parts) ||
-        parts.Length() == 0 /* implies host.Length() == 0 */ ||
-        parts.Length() > 4 ||
-        host[0] == '.') {
+        parts.Length() == 0 ||
+        parts.Length() > 4) {
         return NS_ERROR_FAILURE;
     }
     uint32_t n = 0;
     nsTArray<int32_t> numbers;
     for (uint32_t i = 0; i < parts.Length(); ++i) {
         if (NS_FAILED(ParseIPv4Number(parts[i], n))) {
             return NS_ERROR_FAILURE;
         }