Blame SOURCES/json-c-0.14-fix_usage_of_errno_in_json_parse_uint64.patch

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