From 024b1a8d5493ef13a28ecf7b8e4e0144189311f7 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sun, 16 Dec 2018 15:16:06 +1030 Subject: [PATCH] json: json_to_int helper. Plugins need this for decoding the error code, for example (we only had unsigned handling). Signed-off-by: Rusty Russell --- common/json.c | 23 +++++++++++++++++++++++ common/json.h | 3 +++ 2 files changed, 26 insertions(+) diff --git a/common/json.c b/common/json.c index 50f444fb8..81e1aa06e 100644 --- a/common/json.c +++ b/common/json.c @@ -88,6 +88,29 @@ bool json_to_number(const char *buffer, const jsmntok_t *tok, return true; } +bool json_to_int(const char *buffer, const jsmntok_t *tok, int *num) +{ + char *end; + long l; + + l = strtol(buffer + tok->start, &end, 0); + if (end != buffer + tok->end) + return false; + + BUILD_ASSERT(sizeof(l) >= sizeof(*num)); + *num = l; + + /* Check for overflow/underflow */ + if ((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE) + return false; + + /* Check for truncation */ + if (*num != l) + return false; + + return true; +} + bool json_to_bitcoin_amount(const char *buffer, const jsmntok_t *tok, uint64_t *satoshi) { diff --git a/common/json.h b/common/json.h index 6ba711e8c..170af0420 100644 --- a/common/json.h +++ b/common/json.h @@ -37,6 +37,9 @@ bool json_to_u64(const char *buffer, const jsmntok_t *tok, /* Extract double from this (must be a number literal) */ bool json_to_double(const char *buffer, const jsmntok_t *tok, double *num); +/* Extract signed integer from this (may be a string, or a number literal) */ +bool json_to_int(const char *buffer, const jsmntok_t *tok, int *num); + /* Extract satoshis from this (may be a string, or a decimal number literal) */ bool json_to_bitcoin_amount(const char *buffer, const jsmntok_t *tok, uint64_t *satoshi);