Skip to content

common: reject non-JSON numbers in json_to_s64 and json_to_double - #9756

Open
D1L5H4N wants to merge 1 commit into
ElementsProject:masterfrom
D1L5H4N:fix-json-strict-numbers
Open

D1L5H4N wants to merge 1 commit into
ElementsProject:masterfrom
D1L5H4N:fix-json-strict-numbers

Conversation

@D1L5H4N

@D1L5H4N D1L5H4N commented Nov 23, 2026

Copy link
Copy Markdown

Summary

Resolves #9377.

Tightens number parsing in json_to_s64 and json_to_double according to the JSON specification (RFC 8259):

  • json_to_s64: explicitly rejects leading + signs (+1, +0, +1555).
  • json_to_double: explicitly rejects leading +, requires an optional - followed immediately by a digit, and rejects hexadecimal floating points (0x...), inf, and nan.
  • common/test/run-json.c: adds negative test assertions for json_to_s64 and json_to_u64, and introduces comprehensive unit tests for json_to_double covering valid numbers and invalid non-JSON inputs.

Changelog-None

Tighten number parsing in json_to_s64 and json_to_double to conform
strictly to the JSON specification (RFC 8259). Explicitly reject
leading '+' signs in json_to_s64, and reject leading '+', hexadecimal
floats, and special values like 'inf' or 'nan' in json_to_double.

Add unit tests in common/test/run-json.c covering both valid formats
and invalid non-JSON inputs.

Changelog-None
Fixes ElementsProject#9377
@daywalker90
daywalker90 force-pushed the fix-json-strict-numbers branch from 805e9c0 to 98b2316 Compare November 30, 2026 15:39
char *end;

/* JSON numbers cannot have an explicit '+' sign or empty string. */
if (p[0] == '\0' || p[0] == '+')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is redundant, the following two if's already cause these to retrun false.

if (*p == '-')
p++;

/* Must begin with a digit (rejects 'inf', 'nan', '0x...', etc.) */

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the 0 in 0x is a digit and would be accepted here. 0x would be rejected later though

p++;

/* Must begin with a digit (rejects 'inf', 'nan', '0x...', etc.) */
if (*p < '0' || *p > '9')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cisdigit(*p) is preferred here

@daywalker90

Copy link
Copy Markdown
Collaborator
  1. run-json: common/test/run-json.c:280: void do_json_to_s64(const char *, _Bool, s64): Assertion 'toks' failed.

CLN builds jsmn with -DJSMN_STRICT=1, which only permits unquoted primitives starting with '-', '0'..'9', 't', 'f', or 'n' (jsmn.c:261-289). Consequently, json_parse_simple() returns NULL for unquoted +1, +0, +1555, +, +1.5, inf, INF, and NAN, crashing assert(toks).

Inputs rejected by JSMN_STRICT as unquoted primitives need to be tested as JSON strings (e.g. "\"+1\"", "\"inf\"", "\"NAN\""), while primitives jsmn does pass through (nan, -inf, -nan, 0x1.0p0, 0x10, -0x1.0p0, -.5) can be tested unquoted.

  1. json_to_s64 still accepts leading whitespace, bypassing the + check

Check the prefix checks in json_to_double for this.

  1. The commit message states that json_to_s64 and json_to_double are tightened "to conform strictly to the JSON specification (RFC 8259)", but json_to_double still lets non-JSON numbers through to strtod:
    • Trailing decimal point with no fractional digits: 1., -0., 1.e3, 1.E+2
    • Leading zeros: 00, 01, -01, 01.5

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

common: json_to_s64 and json_to_double still accept hex, octal and signs

2 participants