Skip to content

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

Open
iliaal wants to merge 1 commit into
php:masterfrom
iliaal:fix/json-scanner-counter-overflow
Open

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

Conversation

@iliaal

@iliaal iliaal commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant