]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Merge remote-tracking branch 'origin/maint-0.2.2'
authorNick Mathewson <nickm@torproject.org>
Thu, 10 May 2012 19:41:04 +0000 (15:41 -0400)
committerNick Mathewson <nickm@torproject.org>
Thu, 10 May 2012 19:41:04 +0000 (15:41 -0400)
Conflicts:
src/common/util.c
src/test/test_util.c

1  2 
src/common/util.c
src/test/test_util.c

index 276c6dd13b3dad2df91c29757745811fa201e7dd,7d2fc4dea8c2ff15e1b5ecbc5c096991d799d7e2..30bf6879ac64c48e2d6ae25edcda0d8777c2a7a7
@@@ -906,11 -803,7 +909,13 @@@ tor_parse_long(const char *s, int base
    char *endptr;
    long r;
  
 +  if (base < 0) {
 +    if (ok)
 +      *ok = 0;
 +    return 0;
 +  }
++
+   errno = 0;
    r = strtol(s, &endptr, base);
    CHECK_STRTOX_RESULT();
  }
@@@ -923,11 -816,7 +928,13 @@@ tor_parse_ulong(const char *s, int base
    char *endptr;
    unsigned long r;
  
 +  if (base < 0) {
 +    if (ok)
 +      *ok = 0;
 +    return 0;
 +  }
++
+   errno = 0;
    r = strtoul(s, &endptr, base);
    CHECK_STRTOX_RESULT();
  }
@@@ -952,15 -842,10 +960,16 @@@ tor_parse_uint64(const char *s, int bas
    char *endptr;
    uint64_t r;
  
 +  if (base < 0) {
 +    if (ok)
 +      *ok = 0;
 +    return 0;
 +  }
 +
+   errno = 0;
  #ifdef HAVE_STRTOULL
    r = (uint64_t)strtoull(s, &endptr, base);
 -#elif defined(MS_WINDOWS)
 +#elif defined(_WIN32)
  #if defined(_MSC_VER) && _MSC_VER < 1300
    tor_assert(base <= 10);
    r = (uint64_t)_atoi64(s);
index 7a455e06a911820e5e90e164cafeee04c05c1809,ee745c5cf09544041f83757fcb2fb9fa40f13c91..e8f043a2c56991264e5b086967d6f4876435f091
@@@ -731,47 -263,51 +731,62 @@@ test_util_strmisc(void
    test_streq(cp, "");
    test_assert(U64_LITERAL(0) ==
                tor_parse_uint64("12345678901",10,500,INT32_MAX, &i, &cp));
 -  test_assert(i == 0);
 +  test_eq(0, i);
  
    {
 -  /* Test tor_parse_double. */
 +  /* Test parse_double */
    double d = tor_parse_double("10", 0, UINT64_MAX,&i,NULL);
 -  test_assert(i == 1);
 +  test_eq(1, i);
    test_assert(DBL_TO_U64(d) == 10);
    d = tor_parse_double("0", 0, UINT64_MAX,&i,NULL);
 -  test_assert(i == 1);
 +  test_eq(1, i);
    test_assert(DBL_TO_U64(d) == 0);
    d = tor_parse_double(" ", 0, UINT64_MAX,&i,NULL);
 -  test_assert(i == 0);
 +  test_eq(0, i);
    d = tor_parse_double(".0a", 0, UINT64_MAX,&i,NULL);
 -  test_assert(i == 0);
 +  test_eq(0, i);
    d = tor_parse_double(".0a", 0, UINT64_MAX,&i,&cp);
 -  test_assert(i == 1);
 +  test_eq(1, i);
    d = tor_parse_double("-.0", 0, UINT64_MAX,&i,NULL);
 -  test_assert(i == 1);
 +  test_eq(1, i);
 +  test_assert(DBL_TO_U64(d) == 0);
 +  d = tor_parse_double("-10", -100.0, 100.0,&i,NULL);
 +  test_eq(1, i);
 +  test_eq(-10.0, d);
    }
  
 -  /* Test failing snprintf cases */
+   {
+     /* Test tor_parse_* where we overflow/underflow the underlying type. */
+     /* This string should overflow 64-bit ints. */
+ #define TOOBIG "100000000000000000000000000"
+     test_eq(0L, tor_parse_long(TOOBIG, 10, LONG_MIN, LONG_MAX, &i, NULL));
+     test_eq(i, 0);
+     test_eq(0L, tor_parse_long("-"TOOBIG, 10, LONG_MIN, LONG_MAX, &i, NULL));
+     test_eq(i, 0);
+     test_eq(0UL, tor_parse_ulong(TOOBIG, 10, 0, ULONG_MAX, &i, NULL));
+     test_eq(i, 0);
+     test_eq(U64_LITERAL(0), tor_parse_uint64(TOOBIG, 10,
+                                              0, UINT64_MAX, &i, NULL));
+     test_eq(i, 0);
+   }
 +  /* Test snprintf */
 +  /* Returning -1 when there's not enough room in the output buffer */
    test_eq(-1, tor_snprintf(buf, 0, "Foo"));
    test_eq(-1, tor_snprintf(buf, 2, "Foo"));
 -
 -  /* Test printf with uint64 */
 +  test_eq(-1, tor_snprintf(buf, 3, "Foo"));
 +  test_neq(-1, tor_snprintf(buf, 4, "Foo"));
 +  /* Always NUL-terminate the output */
 +  tor_snprintf(buf, 5, "abcdef");
 +  test_eq(0, buf[4]);
 +  tor_snprintf(buf, 10, "abcdef");
 +  test_eq(0, buf[6]);
 +  /* uint64 */
    tor_snprintf(buf, sizeof(buf), "x!"U64_FORMAT"!x",
                 U64_PRINTF_ARG(U64_LITERAL(12345678901)));
 -  test_streq(buf, "x!12345678901!x");
 +  test_streq("x!12345678901!x", buf);
  
 -  /* Test for strcmpstart and strcmpend. */
 +  /* Test str{,case}cmpstart */
    test_assert(strcmpstart("abcdef", "abcdef")==0);
    test_assert(strcmpstart("abcdef", "abc")==0);
    test_assert(strcmpstart("abcdef", "abd")<0);