Bug 1254061 - Rewrite nsHttp::ParseInt64 using strtoll r=mcmanus
authorValentin Gosu <valentin.gosu@gmail.com>
Wed, 23 Mar 2016 13:42:12 +0100
changeset 290046 c143be9df73e9f540d8d406590e934afb8dea3de
parent 290045 8535142add0fce0b838b944dfdab767e143a82ac
child 290047 1686be775813bb687d3e015313f85957ee28af59
push id30114
push usercbook@mozilla.com
push dateThu, 24 Mar 2016 15:15:54 +0000
treeherdermozilla-central@24c5fbde4488 [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 using strtoll 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
@@ -7,16 +7,17 @@
 // HttpLog.h should generally be included first
 #include "HttpLog.h"
 
 #include "nsHttp.h"
 #include "PLDHashTable.h"
 #include "mozilla/Mutex.h"
 #include "mozilla/HashFunctions.h"
 #include "nsCRT.h"
+#include <errno.h>
 
 namespace mozilla {
 namespace net {
 
 // define storage for all atoms
 #define HTTP_ATOM(_name, _value) nsHttpAtom nsHttp::_name = { _value };
 #include "nsHttpAtomList.h"
 #undef HTTP_ATOM
@@ -289,29 +290,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;
 }