summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrond Norbye <trond.norbye@gmail.com>2011-09-03 05:29:10 +0200
committerTrond Norbye <trond.norbye@gmail.com>2011-09-03 05:29:10 +0200
commit51c8f31fc709d06c479de88bdc5e14e757faabc5 (patch)
tree648026fd064ad7505785a28610361cba14bfe796
parent9d871c0e0c95745726f205f38faaf4d6529fd5e8 (diff)
downloadmemcached-51c8f31fc709d06c479de88bdc5e14e757faabc5.tar.gz
Issue 221: Increment treats leading spaces as 0
-rw-r--r--testapp.c4
-rw-r--r--util.c14
2 files changed, 14 insertions, 4 deletions
diff --git a/testapp.c b/testapp.c
index cdccd59..5d52d18 100644
--- a/testapp.c
+++ b/testapp.c
@@ -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
diff --git a/util.c b/util.c
index 95f51fa..cbb0352 100644
--- a/util.c
+++ b/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;