From 0681609c55db18ae6c6a9a49baa3c06637eb593e Mon Sep 17 00:00:00 2001 From: Matt Whitlock Date: Sat, 12 Jul 2025 12:15:31 -0400 Subject: [PATCH] common/json_parse_simple: drop redundant and wrong json_str_to_u64() The json_str_to_u64() function contains incorrect logic. It chops one character off of the beginning and end of the JSMN token and then parses the remainder as a u64, but JSMN_STRING tokens already do not include the enclosing quotation marks, so json_str_to_u64() would actually parse the JSON string "1234" into the integer 23. Oops! Also note that it would simply fail on all input strings shorter than two characters since tok->end would wind up *before* tok->start. Just drop the function entirely. It was only used in one place, and that place explicitly doesn't care whether its input is a JSON number or a numeric string, and it was already calling json_to_u64() as an alternative, and that function already accepts both JSON strings and JSON numbers as input, so the call to json_str_to_u64() would have been entirely redundant if it had been correct. Changelog-Fixed: The `keysend` command no longer corrupts the type numbers of extra TLVs when they are specified as numeric strings longer than 2 digits. --- common/json_param.c | 3 +-- common/json_parse_simple.c | 13 ------------- common/json_parse_simple.h | 4 ---- 3 files changed, 1 insertion(+), 19 deletions(-) diff --git a/common/json_param.c b/common/json_param.c index b95952dd48be..c9e3070e3e4b 100644 --- a/common/json_param.c +++ b/common/json_param.c @@ -975,8 +975,7 @@ struct command_result *param_extra_tlvs(struct command *cmd, const char *name, /* Accept either bare ints as keys (not spec * compliant, but simpler), or ints in strings, which * are JSON spec compliant. */ - if (!(json_str_to_u64(buffer, curr, &f->numtype) || - json_to_u64(buffer, curr, &f->numtype))) { + if (!json_to_u64(buffer, curr, &f->numtype)) { return command_fail( cmd, JSONRPC2_INVALID_PARAMS, "\"%s\" is not a valid numeric TLV type.", diff --git a/common/json_parse_simple.c b/common/json_parse_simple.c index 7f9c630ebeb2..53e9470ca711 100644 --- a/common/json_parse_simple.c +++ b/common/json_parse_simple.c @@ -112,19 +112,6 @@ bool json_to_s64(const char *buffer, const jsmntok_t *tok, s64 *num) return true; } -bool json_str_to_u64(const char *buffer, const jsmntok_t *tok, u64 *num) -{ - jsmntok_t temp; - if (tok->type != JSMN_STRING) - return false; - - temp = *tok; - temp.start += 1; - temp.end -= 1; - - return json_to_u64(buffer, &temp, num); -} - bool json_to_double(const char *buffer, const jsmntok_t *tok, double *num) { char *end; diff --git a/common/json_parse_simple.h b/common/json_parse_simple.h index 710520d6f327..6b5a2b068567 100644 --- a/common/json_parse_simple.h +++ b/common/json_parse_simple.h @@ -38,10 +38,6 @@ bool json_to_u64(const char *buffer, const jsmntok_t *tok, u64 *num); /* Extract signed 64 bit integer from this (may be a string, or a number literal) */ bool json_to_s64(const char *buffer, const jsmntok_t *tok, s64 *num); -/* Extract number from string. The number must be the entirety of the - * string between the '"' */ -bool json_str_to_u64(const char *buffer, const jsmntok_t *tok, u64 *num); - /* Extract number from this (may be a string, or a number literal) */ bool json_to_u32(const char *buffer, const jsmntok_t *tok, u32 *num);