Commit Graph

9 Commits

Author SHA1 Message Date
Matt Whitlock
71ddba283f 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.
2025-09-22 10:16:51 +09:30
Matt Whitlock
ea2feccbf8 common: set errno=0 before calling strto{l,ul,ull}
The strto{l,ul,ull} functions do not set errno upon a successful return, so a
successful return from a maximally valued input could be misinterpreted as an
overflow error if errno happened already to be set to ERANGE before the call.
To guard against this edge case, always set errno to zero before calling these
functions if checking errno afterward.

Changelog-None
2025-08-15 10:15:16 +09:30
Matt Whitlock
9112c1d518 common/json_parse_simple: make convenience functions inline
json_tok_streq(…) and json_get_member(…) are convenience wrappers for
json_tok_strneq(…) and json_get_membern(…) respectively. Unfortunately, using
them incurs a performance penalty in the common case where they are called with
a string literal argument because the compiler is unable to substitute a
compile-time constant in place of the buried call to strlen(…).

For example,

	json_get_member(buf, tok, "example");

…will have worse performance than…

	json_get_membern(buf, tok, "example", strlen("example"));

…because the former is forced to scan over "example" at run-time to count its
length whereas the latter is able to elide the strlen(…) call at compile time.

Hoist these convenience functions up into common/json_parse_simple.h and mark
them as inline so that the compiler can elide the strlen(…) call in the common
case of calling these functions with a string literal argument.

Changelog-None
2025-08-14 17:53:39 +09:30
Dustin Dettmer
aba4d18ed1 signed types: add handlers for signed types
We're adding signed types to the spec! This adds the support mechanisms
for them.
2023-07-13 19:34:47 +09:30
Rusty Russell
9589ea0240 common: add routine to get double from JSON.
I don't like it, but we do expose some times like this :(

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-01-30 15:15:41 -06:00
Rusty Russell
2d8fff6b57 libplugin: don't turn non-string JSON ids into strings.
When called with `"id": 1` we replied with `"id": "1"`.  lightningd doesn't
actually care, but it's weird.

Copy the entire token: this way we don't have to special case anything.

Also, remove the doubled test in json_add_jsonstr.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-01-11 11:13:27 +10:30
Christian Decker
83beaa5396 json: Add helper for quoted numbers
The JSON specification technically disallows maps with numeric keys,
so we'll want to slowly migrate away from using them. This helper
extracts the numeric value from a quoted number, which is a legal
representation of the same in JSON.
2022-11-01 17:05:30 +01:00
Rusty Russell
88354b79bd common: helper to get id field as a string.
We'll be doing this quite a bit, so provide common helper.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2022-09-16 12:31:45 +09:30
Rusty Russell
401f1debc5 common: clean up json routine locations.
We have them split over common/param.c, common/json.c,
common/json_helpers.c, common/json_tok.c and common/json_stream.c.

Change that to:
* common/json_parse (all the json_to_xxx routines)
* common/json_parse_simple (simplest the json parsing routines, for cli too)
* common/json_stream (all the json_add_xxx routines)
* common/json_param (all the param and param_xxx routines)

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2022-07-15 12:24:00 -05:00