From d0f8912c2702f31b26b76c7dc031997b50574cd2 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Thu, 12 May 2022 13:32:40 -0400 Subject: [PATCH] fix: parse_uint64_t overflow handling --- runtime/include/json.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/runtime/include/json.h b/runtime/include/json.h index 25e6b00..547a686 100644 --- a/runtime/include/json.h +++ b/runtime/include/json.h @@ -1,6 +1,6 @@ #pragma once - +#include #include #include #include @@ -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; }