|
|
a0bbcb |
From 003b58782b12798da3da8b952152988a88dfb532 Mon Sep 17 00:00:00 2001
|
|
|
a0bbcb |
From: Pierce Lopez <pierce.lopez@gmail.com>
|
|
|
a0bbcb |
Date: Sun, 10 May 2020 13:20:02 -0400
|
|
|
a0bbcb |
Subject: [PATCH] fix json_parse_uint64() usage of errno
|
|
|
a0bbcb |
|
|
|
a0bbcb |
introduced in #542
|
|
|
a0bbcb |
fixes #601
|
|
|
a0bbcb |
---
|
|
|
a0bbcb |
json_util.c | 8 +++-----
|
|
|
a0bbcb |
json_util.h | 1 +
|
|
|
a0bbcb |
tests/test_parse_int64.expected | 8 ++++----
|
|
|
a0bbcb |
3 files changed, 8 insertions(+), 9 deletions(-)
|
|
|
a0bbcb |
|
|
|
a0bbcb |
diff --git a/json_util.c b/json_util.c
|
|
|
a0bbcb |
index d3ee47df72..e8e2ec6bcb 100644
|
|
|
a0bbcb |
--- a/json_util.c
|
|
|
a0bbcb |
+++ b/json_util.c
|
|
|
a0bbcb |
@@ -245,19 +245,17 @@ int json_parse_uint64(const char *buf, uint64_t *retval)
|
|
|
a0bbcb |
{
|
|
|
a0bbcb |
char *end = NULL;
|
|
|
a0bbcb |
uint64_t val;
|
|
|
a0bbcb |
- errno = 1;
|
|
|
a0bbcb |
|
|
|
a0bbcb |
+ errno = 0;
|
|
|
a0bbcb |
while (*buf == ' ')
|
|
|
a0bbcb |
- {
|
|
|
a0bbcb |
buf++;
|
|
|
a0bbcb |
- }
|
|
|
a0bbcb |
if (*buf == '-')
|
|
|
a0bbcb |
- errno = 0;
|
|
|
a0bbcb |
+ return 1; /* error: uint cannot be negative */
|
|
|
a0bbcb |
|
|
|
a0bbcb |
val = strtoull(buf, &end, 10);
|
|
|
a0bbcb |
if (end != buf)
|
|
|
a0bbcb |
*retval = val;
|
|
|
a0bbcb |
- return ((errno == 0) || (end == buf)) ? 1 : 0;
|
|
|
a0bbcb |
+ return ((val == 0 && errno != 0) || (end == buf)) ? 1 : 0;
|
|
|
a0bbcb |
}
|
|
|
a0bbcb |
|
|
|
a0bbcb |
#ifndef HAVE_REALLOC
|
|
|
a0bbcb |
diff --git a/json_util.h b/json_util.h
|
|
|
a0bbcb |
index 2a4b6c19bd..7520f036c4 100644
|
|
|
a0bbcb |
--- a/json_util.h
|
|
|
a0bbcb |
+++ b/json_util.h
|
|
|
a0bbcb |
@@ -100,6 +100,7 @@ JSON_EXPORT int json_object_to_fd(int fd, struct json_object *obj, int flags);
|
|
|
a0bbcb |
*/
|
|
|
a0bbcb |
JSON_EXPORT const char *json_util_get_last_err(void);
|
|
|
a0bbcb |
|
|
|
a0bbcb |
+/* these parsing helpers return zero on success */
|
|
|
a0bbcb |
JSON_EXPORT int json_parse_int64(const char *buf, int64_t *retval);
|
|
|
a0bbcb |
JSON_EXPORT int json_parse_uint64(const char *buf, uint64_t *retval);
|
|
|
a0bbcb |
JSON_EXPORT int json_parse_double(const char *buf, double *retval);
|
|
|
a0bbcb |
diff --git a/tests/test_parse_int64.expected b/tests/test_parse_int64.expected
|
|
|
a0bbcb |
index f4c5750b0b..6dca94b470 100644
|
|
|
a0bbcb |
--- a/tests/test_parse_int64.expected
|
|
|
a0bbcb |
+++ b/tests/test_parse_int64.expected
|
|
|
a0bbcb |
@@ -34,13 +34,13 @@ buf=123 parseit=0, value=123
|
|
|
a0bbcb |
==========json_parse_uint64() test===========
|
|
|
a0bbcb |
buf=x parseit=1, value=666
|
|
|
a0bbcb |
buf=0 parseit=0, value=0
|
|
|
a0bbcb |
-buf=-0 parseit=1, value=0
|
|
|
a0bbcb |
+buf=-0 parseit=1, value=666
|
|
|
a0bbcb |
buf=00000000 parseit=0, value=0
|
|
|
a0bbcb |
-buf=-00000000 parseit=1, value=0
|
|
|
a0bbcb |
+buf=-00000000 parseit=1, value=666
|
|
|
a0bbcb |
buf=1 parseit=0, value=1
|
|
|
a0bbcb |
buf=2147483647 parseit=0, value=2147483647
|
|
|
a0bbcb |
-buf=-1 parseit=1, value=18446744073709551615
|
|
|
a0bbcb |
-buf=-9223372036854775808 parseit=1, value=9223372036854775808
|
|
|
a0bbcb |
+buf=-1 parseit=1, value=666
|
|
|
a0bbcb |
+buf=-9223372036854775808 parseit=1, value=666
|
|
|
a0bbcb |
buf= 1 parseit=0, value=1
|
|
|
a0bbcb |
buf=00001234 parseit=0, value=1234
|
|
|
a0bbcb |
buf=0001234x parseit=0, value=1234
|