Skip to content

Fix integer overflow in the JSON scanner string length adjustments - #195

Closed
iliaal wants to merge 1 commit into
masterfrom
fix/json-scanner-counter-overflow
Closed

Fix integer overflow in the JSON scanner string length adjustments#195
iliaal wants to merge 1 commit into
masterfrom
fix/json-scanner-counter-overflow

Conversation

@iliaal

@iliaal iliaal commented Jul 29, 2026

Copy link
Copy Markdown
Owner

php_json_scanner keeps escape shrinkage and invalid-UTF-8 adjustment in int fields. A string carrying more than 2^31 escape sequences or ignored invalid bytes wraps the counter, and the first pass then reserves a result several gigabytes longer than the second pass writes. json_decode() of a 2 GiB run of invalid bytes with JSON_INVALID_UTF8_IGNORE returns a 4 GiB string instead of an empty one, and 2.4 GiB of A escapes returns 4.4 GiB instead of 409 MiB. There is no test because the smallest trigger needs roughly 10 GiB of live memory. This targets master rather than 8.4 since php_json_scanner.h is an installed header whose struct is embedded by value in php_json_parser, so widening the fields changes the layout for anything that embeds it.

The scanner accumulates escape shrinkage and invalid-UTF-8 adjustment in
int fields, so a string carrying more than 2^31 escape sequences or
ignored invalid bytes wraps the counter and makes the first pass reserve
a result several gigabytes longer than the second pass writes.
json_decode() of a 2 GiB run of invalid bytes with
JSON_INVALID_UTF8_IGNORE returns a 4 GiB string instead of an empty one.
Widen both counters to ptrdiff_t, which is what the token length they
adjust is already measured in.

Closes phpGH-22920
@iliaal
iliaal force-pushed the fix/json-scanner-counter-overflow branch from 2c4473b to 06eda50 Compare July 29, 2026 12:49
@iliaal

iliaal commented Jul 29, 2026

Copy link
Copy Markdown
Owner Author

Promoted upstream as php#22920.

@iliaal iliaal closed this Jul 29, 2026
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.

1 participant