Skip to content

Fix GH-19088: Make sscanf %c read whitespace#22041

Open
prateekbhujel wants to merge 3 commits into
php:masterfrom
prateekbhujel:prateekbhujel/fix-gh-19088-sscanf-c-whitespace
Open

Fix GH-19088: Make sscanf %c read whitespace#22041
prateekbhujel wants to merge 3 commits into
php:masterfrom
prateekbhujel:prateekbhujel/fix-gh-19088-sscanf-c-whitespace

Conversation

@prateekbhujel
Copy link
Copy Markdown
Contributor

Fixes GH-19088.

%c already disables the usual leading whitespace skip, but after that it still reused the %s scan path, which stops as soon as it sees whitespace. That made sscanf(' ', '%c%n', ...) assign an empty string and leave the offset at 0.

Keep %c separate from %s in the scan loop so it consumes the requested number of characters, including spaces. The same scanner is used by fscanf(), so the existing %c expectations there are updated as well.

Tests run:

sapi/cli/php run-tests.php -q ext/standard/tests/strings/gh19088.phpt
sapi/cli/php run-tests.php -q ext/standard/tests/strings/gh19088.phpt ext/standard/tests/strings/sscanf_basic4.phpt ext/standard/tests/strings/bug21730.phpt ext/standard/tests/file/fscanf_variation20.phpt ext/standard/tests/file/fscanf_variation21.phpt ext/standard/tests/file/fscanf_variation22.phpt ext/standard/tests/file/fscanf_variation23.phpt ext/standard/tests/file/fscanf_variation24.phpt ext/standard/tests/file/fscanf_variation25.phpt ext/standard/tests/file/fscanf_variation26.phpt
git diff --check

Keep the %c conversion separate from %s so it reads whitespace instead of stopping at it. The %c conversion already suppresses leading whitespace skipping; it also must not treat whitespace as the end of the field.
Comment thread ext/standard/tests/strings/gh19088.phpt
Comment thread ext/standard/scanf.c
@prateekbhujel prateekbhujel force-pushed the prateekbhujel/fix-gh-19088-sscanf-c-whitespace branch from acdb609 to 08ea8fb Compare May 14, 2026 21:16
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.

sscanf c conversion specifier

2 participants