Bug 1254061 - Rewrite nsHttp::ParseInt64 r=mcmanus
☠☠ backed out by 52c1e3e2fcfb ☠ ☠
authorValentin Gosu <valentin.gosu@gmail.com>
Mon, 21 Mar 2016 17:57:02 +0100
changeset 289680 74312d99d538b934c2d47e32f3e272bc78d3208a
parent 289679 56b2ee9b6429a2bf935f012d7e75d3e38a1ed747
child 289681 96e8d52b4b017e9b9e9a210683dc837a2025f5f8
push id30108
push usercbook@mozilla.com
push dateTue, 22 Mar 2016 11:14:31 +0000
treeherdermozilla-central@ea6298e1b4f7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmcmanus
bugs1254061
milestone48.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 1254061 - Rewrite nsHttp::ParseInt64 r=mcmanus MozReview-Commit-ID: FXjTBah4OSd * * * [mq]: test MozReview-Commit-ID: 5YT1jWVb21K
netwerk/protocol/http/nsHttp.cpp
--- a/netwerk/protocol/http/nsHttp.cpp
+++ b/netwerk/protocol/http/nsHttp.cpp
@@ -289,29 +289,35 @@ nsHttp::FindToken(const char *input, con
     }
 
     return nullptr;
 }
 
 bool
 nsHttp::ParseInt64(const char *input, const char **next, int64_t *r)
 {
-    const char *start = input;
-    *r = 0;
-    while (*input >= '0' && *input <= '9') {
-        int64_t next = 10 * (*r) + (*input - '0');
-        if (next < *r) // overflow?
-            return false;
-        *r = next;
-        ++input;
+    MOZ_ASSERT(input);
+    MOZ_ASSERT(r);
+
+    char *end = nullptr;
+    errno = 0; // Clear errno to make sure its value is set by strtoll
+    int64_t value = strtoll(input, &end, /* base */ 10);
+
+    // Fail if: - the parsed number overflows.
+    //          - the end points to the start of the input string.
+    //          - we parsed a negative value. Consumers don't expect that.
+    if (errno != 0 || end == input || value < 0) {
+        LOG(("nsHttp::ParseInt64 value=%ld errno=%d", value, errno));
+        return false;
     }
-    if (input == start) // nothing parsed?
-        return false;
-    if (next)
-        *next = input;
+
+    if (next) {
+        *next = end;
+    }
+    *r = value;
     return true;
 }
 
 bool
 nsHttp::IsPermanentRedirect(uint32_t httpStatus)
 {
   return httpStatus == 301 || httpStatus == 308;
 }