Fix integer overflow in the JSON scanner string length adjustments - #22920
Open
iliaal wants to merge 1 commit into
Open
Fix integer overflow in the JSON scanner string length adjustments#22920iliaal wants to merge 1 commit into
iliaal wants to merge 1 commit into
Conversation
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
force-pushed
the
fix/json-scanner-counter-overflow
branch
from
July 29, 2026 12:49
2c4473b to
06eda50
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.