]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Have tor_parse_*long functions check for negative bases
authorNick Mathewson <nickm@torproject.org>
Fri, 30 Mar 2012 14:34:05 +0000 (10:34 -0400)
committerNick Mathewson <nickm@torproject.org>
Fri, 30 Mar 2012 14:34:05 +0000 (10:34 -0400)
One of our unit tests checks that they behave correctly (giving an
error) when the base is negative.  But there isn't a guarantee that
strtol and friends actually handle negative bases correctly.

Found by Coverity Scan; fix for CID 504.

src/common/util.c

index 1807ee7adc013e1ef2bfa53d8a1c59f85147f45e..266368cc9d0083232cf8646c7ef1d0edc0e2a3f3 100644 (file)
@@ -906,6 +906,11 @@ tor_parse_long(const char *s, int base, long min, long max,
   char *endptr;
   long r;
 
+  if (base < 0) {
+    if (ok)
+      *ok = 0;
+    return 0;
+  }
   r = strtol(s, &endptr, base);
   CHECK_STRTOX_RESULT();
 }
@@ -918,6 +923,11 @@ tor_parse_ulong(const char *s, int base, unsigned long min,
   char *endptr;
   unsigned long r;
 
+  if (base < 0) {
+    if (ok)
+      *ok = 0;
+    return 0;
+  }
   r = strtoul(s, &endptr, base);
   CHECK_STRTOX_RESULT();
 }
@@ -942,6 +952,12 @@ tor_parse_uint64(const char *s, int base, uint64_t min,
   char *endptr;
   uint64_t r;
 
+  if (base < 0) {
+    if (ok)
+      *ok = 0;
+    return 0;
+  }
+
 #ifdef HAVE_STRTOULL
   r = (uint64_t)strtoull(s, &endptr, base);
 #elif defined(_WIN32)