fix: parse_uint64_t overflow handling

master
Sean McBride 3 years ago
parent 013448c716
commit d0f8912c27

@ -1,6 +1,6 @@
#pragma once
#include <errno.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdlib.h>
@ -156,14 +156,18 @@ parse_uint32_t(jsmntok_t tok, const char *json_buf, const char *key, uint32_t *r
static inline int
parse_uint64_t(jsmntok_t tok, const char *json_buf, const char *key, uint64_t *ret)
{
char *end = NULL;
intmax_t temp = strtoimax(&json_buf[tok.start], &end, 10);
if (json_buf[tok.start] == '-') {
fprintf(stderr, "Unable to parse uint64_t for key %s, had negative number\n", key);
}
if (end != &json_buf[tok.end] || temp < 0 || temp > UINT64_MAX) {
errno = 0;
char *end = NULL;
uintmax_t temp = strtoumax(&json_buf[tok.start], &end, 10);
if (end != &json_buf[tok.end] || (temp == UINT64_MAX && errno == ERANGE)) {
fprintf(stderr, "Unable to parse uint64_t for key %s\n", key);
return -1;
}
*ret = (uint64_t)temp;
*ret = temp;
return 0;
}

Loading…
Cancel
Save