common/json_parse_simple: drop redundant and wrong json_str_to_u64()
What changed, and why it matters
This commit fixes a bug in Core Lightning's JSON parsing. A helper function meant to read a number from a quoted JSON string was accidentally stripping one digit from each end. For example, the string "1234" was being read as 23, and single-digit strings failed entirely. The only user of this broken helper was the `keysend` command's handling of extra TLV (custom data) type numbers. As a result, when a user specified extra TLV type numbers as quoted numeric strings of more than two digits, the wrong type number would be stored and sent. This could cause payments to include malformed custom data, potentially making them fail or be misinterpreted by the recipient.
Apply the patch. It is a clean removal of a buggy, redundant function with no functional loss because `json_to_u64()` already handles both JSON strings and numbers. Users running `keysend` with quoted extra TLV type numbers should upgrade to avoid corrupted TLV types.
Security signals we found
Data corruption / incorrect TLV type encoding in keysend payments
Logic error in JSON token boundary handling
Redundant code path masked the bug until code review/refactoring
Potential payment failure or interoperability issue due to malformed extra TLVs
Evidence from the diff
The removed json_str_to_u64() incorrectly assumed JSMN_STRING tokens include surrounding quote characters, so it advanced tok->start by 1 and retreated tok->end by 1 before calling json_to_u64(). JSMN tokens already exclude quotes, so this sliced off the first and last digits of the actual number. The function was only called from param_extra_tlvs() in common/json_param.c, which already fell back to json_to_u64() (which itself accepts both string and numeric JSON tokens). The fix simply removes the broken helper and relies on json_to_u64().
Changed components
common/json_parse_simple.ccommon/json_parse_simple.hcommon/json_param.ckeysend command extra TLV parameter parsingInspect captured patch +1 / −19
diff --git a/common/json_param.c b/common/json_param.c
index 454e6a55..c92de191 100644
--- a/common/json_param.c
+++ b/common/json_param.c
@@ -986,8 +986,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 0842fc58..e3b2cba8 100644
--- a/common/json_parse_simple.c
+++ b/common/json_parse_simple.c
@@ -109,19 +109,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 eb7a70d5..0882812d 100644
--- a/common/json_parse_simple.h
+++ b/common/json_parse_simple.h
@@ -42,10 +42,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);
Why this scored 62/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.