diff options
author | Eric Haszlakiewicz <erh+git@nimenees.com> | 2022-10-30 03:25:32 +0000 |
---|---|---|
committer | Eric Haszlakiewicz <erh+git@nimenees.com> | 2022-10-30 03:25:32 +0000 |
commit | c50bf9df9cb7dacd682104d844029938522f0bda (patch) | |
tree | 42f8126108d7d478918879b83d5ad97795b43adf | |
parent | 57bef5edc48ad30e794f64fdfc855e955046d25d (diff) | |
download | json-c-c50bf9df9cb7dacd682104d844029938522f0bda.tar.gz |
Apply same EINVAL handling to json_parse_uint64() as was done for json_parse_int64(). Document that overflow/underflow for these functions is not an error, but sets errno=ERANGE.
-rw-r--r-- | json_util.c | 7 | ||||
-rw-r--r-- | json_util.h | 11 |
2 files changed, 16 insertions, 2 deletions
diff --git a/json_util.c b/json_util.c index 1a2dfcd..0f570f2 100644 --- a/json_util.c +++ b/json_util.c @@ -269,7 +269,12 @@ int json_parse_uint64(const char *buf, uint64_t *retval) val = strtoull(buf, &end, 10); if (end != buf) *retval = val; - return ((val == 0 && errno != 0) || (end == buf)) ? 1 : 0; + if ((val == 0 && errno != 0) || (end == buf)) + { + errno = EINVAL; + return 1; + } + return 0; } #ifndef HAVE_REALLOC diff --git a/json_util.h b/json_util.h index 1f663e8..62ad688 100644 --- a/json_util.h +++ b/json_util.h @@ -100,8 +100,17 @@ 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 */ +/** + * A parsing helper for integer values. Returns 0 on success, + * with the parsed value assigned to *retval. Overflow/underflow + * are NOT considered errors, but errno will be set to ERANGE, + * just like the strtol/strtoll functions do. + */ JSON_EXPORT int json_parse_int64(const char *buf, int64_t *retval); +/** + * A parsing help for integer values, providing one extra bit of + * magnitude beyond json_parse_int64(). + */ JSON_EXPORT int json_parse_uint64(const char *buf, uint64_t *retval); /** * @deprecated |