diff --git a/daemon/json.c b/daemon/json.c index e878611da..1991ed0c0 100644 --- a/daemon/json.c +++ b/daemon/json.c @@ -1,6 +1,7 @@ /* JSON core and helpers */ #include "json.h" #include +#include #include #include #include @@ -39,8 +40,8 @@ bool json_tok_streq(const char *buffer, const jsmntok_t *tok, const char *str) return strncmp(buffer + tok->start, str, tok->end - tok->start) == 0; } -bool json_tok_number(const char *buffer, const jsmntok_t *tok, - unsigned int *num) +bool json_tok_u64(const char *buffer, const jsmntok_t *tok, + uint64_t *num) { char *end; unsigned long l; @@ -49,6 +50,7 @@ bool json_tok_number(const char *buffer, const jsmntok_t *tok, if (end != buffer + tok->end) return false; + BUILD_ASSERT(sizeof(l) >= sizeof(*num)); *num = l; /* Check for overflow */ @@ -59,7 +61,22 @@ bool json_tok_number(const char *buffer, const jsmntok_t *tok, return false; return true; -} +} + +bool json_tok_number(const char *buffer, const jsmntok_t *tok, + unsigned int *num) +{ + uint64_t u64; + + if (!json_tok_u64(buffer, tok, &u64)) + return false; + *num = u64; + + /* Just in case it doesn't fit. */ + if (*num != u64) + return false; + return true; +} bool json_tok_is_null(const char *buffer, const jsmntok_t *tok) { diff --git a/daemon/json.h b/daemon/json.h index c333803fc..fc990ea1c 100644 --- a/daemon/json.h +++ b/daemon/json.h @@ -1,9 +1,10 @@ #ifndef LIGHTNING_DAEMON_JSON_H #define LIGHTNING_DAEMON_JSON_H #include "config.h" -#include "stdbool.h" -#include "stdlib.h" #include +#include +#include +#include #define JSMN_STRICT 1 # include "jsmn/jsmn.h" @@ -23,6 +24,10 @@ bool json_tok_streq(const char *buffer, const jsmntok_t *tok, const char *str); bool json_tok_number(const char *buffer, const jsmntok_t *tok, unsigned int *num); +/* Extract number from this (may be a string, or a number literal) */ +bool json_tok_u64(const char *buffer, const jsmntok_t *tok, + uint64_t *num); + /* Is this the null primitive? */ bool json_tok_is_null(const char *buffer, const jsmntok_t *tok);