Permalink
Browse files

Issue 221: Increment treats leading spaces as 0

  • Loading branch information...
1 parent 9d871c0 commit 51c8f31fc709d06c479de88bdc5e14e757faabc5 @trondn trondn committed Sep 3, 2011
Showing with 14 additions and 4 deletions.
  1. +4 −0 testapp.c
  2. +10 −4 util.c
View
@@ -190,6 +190,7 @@ static enum test_return test_safe_strtoul(void) {
assert(val == 123);
assert(!safe_strtoul("", &val)); // empty
assert(!safe_strtoul("123BOGUS", &val)); // non-numeric
+ assert(!safe_strtoul(" issue221", &val)); // non-numeric
/* Not sure what it does, but this works with ICC :/
assert(!safe_strtoul("92837498237498237498029383", &val)); // out of range
*/
@@ -214,6 +215,7 @@ static enum test_return test_safe_strtoull(void) {
assert(!safe_strtoull("", &val)); // empty
assert(!safe_strtoull("123BOGUS", &val)); // non-numeric
assert(!safe_strtoull("92837498237498237498029383", &val)); // out of range
+ assert(!safe_strtoull(" issue221", &val)); // non-numeric
// extremes:
assert(safe_strtoull("18446744073709551615", &val)); // 2**64 - 1
@@ -234,6 +236,7 @@ static enum test_return test_safe_strtoll(void) {
assert(!safe_strtoll("", &val)); // empty
assert(!safe_strtoll("123BOGUS", &val)); // non-numeric
assert(!safe_strtoll("92837498237498237498029383", &val)); // out of range
+ assert(!safe_strtoll(" issue221", &val)); // non-numeric
// extremes:
assert(!safe_strtoll("18446744073709551615", &val)); // 2**64 - 1
@@ -262,6 +265,7 @@ static enum test_return test_safe_strtol(void) {
assert(!safe_strtol("", &val)); // empty
assert(!safe_strtol("123BOGUS", &val)); // non-numeric
assert(!safe_strtol("92837498237498237498029383", &val)); // out of range
+ assert(!safe_strtol(" issue221", &val)); // non-numeric
// extremes:
/* This actually works on 64-bit ubuntu
View
14 util.c
@@ -17,8 +17,10 @@ bool safe_strtoull(const char *str, uint64_t *out) {
*out = 0;
char *endptr;
unsigned long long ull = strtoull(str, &endptr, 10);
- if (errno == ERANGE)
+ if ((errno == ERANGE) || (str == endptr)) {
return false;
+ }
+
if (xisspace(*endptr) || (*endptr == '\0' && endptr != str)) {
if ((long long) ull < 0) {
/* only check for negative signs in the uncommon case when
@@ -40,8 +42,10 @@ bool safe_strtoll(const char *str, int64_t *out) {
*out = 0;
char *endptr;
long long ll = strtoll(str, &endptr, 10);
- if (errno == ERANGE)
+ if ((errno == ERANGE) || (str == endptr)) {
return false;
+ }
+
if (xisspace(*endptr) || (*endptr == '\0' && endptr != str)) {
*out = ll;
return true;
@@ -58,7 +62,7 @@ bool safe_strtoul(const char *str, uint32_t *out) {
errno = 0;
l = strtoul(str, &endptr, 10);
- if (errno == ERANGE) {
+ if ((errno == ERANGE) || (str == endptr)) {
return false;
}
@@ -84,8 +88,10 @@ bool safe_strtol(const char *str, int32_t *out) {
*out = 0;
char *endptr;
long l = strtol(str, &endptr, 10);
- if (errno == ERANGE)
+ if ((errno == ERANGE) || (str == endptr)) {
return false;
+ }
+
if (xisspace(*endptr) || (*endptr == '\0' && endptr != str)) {
*out = l;
return true;

0 comments on commit 51c8f31

Please sign in to comment.