commit c78a42685fd342ec961ede7a61e7b82bd40060b8
Merge: 1cf970c 79c4c81
Author: Nick Mathewson <nickm(a)torproject.org>
Date: Thu May 10 15:41:04 2012 -0400
Merge remote-tracking branch 'origin/maint-0.2.2'
Conflicts:
src/common/util.c
src/test/test_util.c
changes/bug5786_range | 8 ++++++++
src/common/util.c | 9 +++++++++
src/test/test_util.c | 15 +++++++++++++++
3 files changed, 32 insertions(+), 0 deletions(-)
diff --cc src/common/util.c
index 276c6dd,7d2fc4d..30bf687
--- a/src/common/util.c
+++ b/src/common/util.c
@@@ -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);
diff --cc src/test/test_util.c
index 7a455e0,ee745c5..e8f043a
--- a/src/test/test_util.c
+++ b/src/test/test_util.c
@@@ -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 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 failing snprintf cases */
+ /* 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);