diff options
author | Eric Hawicz <erh+git@nimenees.com> | 2020-05-10 21:15:47 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-10 21:15:47 -0400 |
commit | 3648c3ed2c598472dc24240a944564c58bc94f84 (patch) | |
tree | cbff6cafc25a3407adc58b00a5b62d4469ba349b | |
parent | 26f080997d41cfdb17beab65e90c82217d0ac43b (diff) | |
parent | 003b58782b12798da3da8b952152988a88dfb532 (diff) | |
download | json-c-3648c3ed2c598472dc24240a944564c58bc94f84.tar.gz |
Merge pull request #602 from ploxiln/parse_uint64_errno
fix json_parse_uint64() usage of errno
-rw-r--r-- | json_util.c | 8 | ||||
-rw-r--r-- | json_util.h | 1 | ||||
-rw-r--r-- | tests/test_parse_int64.expected | 8 |
3 files changed, 8 insertions, 9 deletions
diff --git a/json_util.c b/json_util.c index d3ee47d..e8e2ec6 100644 --- a/json_util.c +++ b/json_util.c @@ -245,19 +245,17 @@ int json_parse_uint64(const char *buf, uint64_t *retval) { char *end = NULL; uint64_t val; - errno = 1; + errno = 0; while (*buf == ' ') - { buf++; - } if (*buf == '-') - errno = 0; + return 1; /* error: uint cannot be negative */ val = strtoull(buf, &end, 10); if (end != buf) *retval = val; - return ((errno == 0) || (end == buf)) ? 1 : 0; + return ((val == 0 && errno != 0) || (end == buf)) ? 1 : 0; } #ifndef HAVE_REALLOC diff --git a/json_util.h b/json_util.h index 2a4b6c1..7520f03 100644 --- a/json_util.h +++ b/json_util.h @@ -100,6 +100,7 @@ JSON_EXPORT int json_object_to_fd(int fd, struct json_object *obj, int flags); */ JSON_EXPORT const char *json_util_get_last_err(void); +/* these parsing helpers return zero on success */ JSON_EXPORT int json_parse_int64(const char *buf, int64_t *retval); JSON_EXPORT int json_parse_uint64(const char *buf, uint64_t *retval); JSON_EXPORT int json_parse_double(const char *buf, double *retval); diff --git a/tests/test_parse_int64.expected b/tests/test_parse_int64.expected index f4c5750..6dca94b 100644 --- a/tests/test_parse_int64.expected +++ b/tests/test_parse_int64.expected @@ -34,13 +34,13 @@ buf=123 parseit=0, value=123 ==========json_parse_uint64() test=========== buf=x parseit=1, value=666 buf=0 parseit=0, value=0 -buf=-0 parseit=1, value=0 +buf=-0 parseit=1, value=666 buf=00000000 parseit=0, value=0 -buf=-00000000 parseit=1, value=0 +buf=-00000000 parseit=1, value=666 buf=1 parseit=0, value=1 buf=2147483647 parseit=0, value=2147483647 -buf=-1 parseit=1, value=18446744073709551615 -buf=-9223372036854775808 parseit=1, value=9223372036854775808 +buf=-1 parseit=1, value=666 +buf=-9223372036854775808 parseit=1, value=666 buf= 1 parseit=0, value=1 buf=00001234 parseit=0, value=1234 buf=0001234x parseit=0, value=1234 |