Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Issue 221: Increment treats leading spaces as 0
  • Loading branch information
trondn committed Sep 3, 2011
1 parent 9d871c0 commit 51c8f31
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
4 changes: 4 additions & 0 deletions testapp.c
Expand Up @@ -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
*/
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions util.c
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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;
}

Expand All @@ -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;
Expand Down

0 comments on commit 51c8f31

Please sign in to comment.