-
Notifications
You must be signed in to change notification settings - Fork 575
Truncate sscanf/fscanf format string at NUL byte before counting placeholders
#5591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
phpstan-bot
wants to merge
7
commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-4vty9bu
Closed
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
25d50a1
Truncate `sscanf`/`fscanf` format string at NUL byte before counting …
phpstan-bot c4b7175
Add missing `%n`, `%i`, `%X` specifiers to sscanf return type inference
phpstan-bot 16b970d
Add missing `%g` and `%D` sscanf specifiers to return type inference …
phpstan-bot 24cc3b7
Fix `%u` sscanf specifier return type to `int|string`
phpstan-bot fea9027
Add comprehensive sscanf format validation test with C ValidateFormat…
phpstan-bot f7a725d
Handle `l`/`L`/`h` scanf size modifiers in format parsing
phpstan-bot ab39be5
Remove massive SscanfFormatValidationTest
phpstan-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
|
|
||
| namespace Bug14567; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| function sscanfNulTerminator(string $s) { | ||
| // NUL byte terminates sscanf format string - placeholders after \0 are ignored | ||
| assertType('array{int|null}|null', sscanf($s, "%d\0%d")); | ||
| assertType('array{int|null, string|null}|null', sscanf($s, "%d %s\0%d")); | ||
| assertType('array{}|null', sscanf($s, "\0%d%s")); | ||
| } | ||
|
|
||
| function fscanfNulTerminator($r) { | ||
| // Same for fscanf | ||
| assertType('array{int|null}|null', fscanf($r, "%d\0%d")); | ||
| assertType('array{int|null, string|null}|null', fscanf($r, "%d %s\0%d")); | ||
| assertType('array{}|null', fscanf($r, "\0%d%s")); | ||
| } |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?php | ||
|
|
||
| namespace Bug14567; | ||
|
|
||
| // NUL byte terminates sscanf/fscanf format string parsing | ||
| // Placeholders after \0 should not be counted | ||
|
|
||
| // Only 1 placeholder active before NUL | ||
| sscanf('123 456', "%d\0%d", $a); | ||
|
|
||
| // Only 1 placeholder active before NUL (fscanf) | ||
| fscanf(STDIN, "%d\0%d", $a2); | ||
|
|
||
| // No placeholders after NUL | ||
| sscanf('123', "\0%d"); | ||
|
|
||
| // Multiple placeholders, NUL in middle | ||
| sscanf('123 456 789', "%d %d\0%d", $b, $c); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#hakre memo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pushed. Here's a summary of what was done to address hakre's review:
Changes made:
SscanfFunctionDynamicReturnTypeExtension.php— Added missing scanf specifiers%i(integer with base detection),%n(characters consumed count), and%X(uppercase hex) to both the regex pattern and the IntegerType mapping. These were already supported inPrintfHelperfor parameter count validation but were missing from the return type inference.Test coverage for hakre's edge cases — Added tests verifying:
array{}|null%nspecifier →array{int|null}|null%%literal percent →array{}|null%ispecifier →array{int|null}|null%Xspecifier →array{int|null}|null%d%n→array{int|null, int|null}|null%n,%%,%iparameter count validationRegarding hakre's suggestion to use
sscanf('', $format)instead of NUL truncation: The NUL truncation approach is preferred because (a) the return type extension needs to know individual specifier types (%d→ int,%s→ string, etc.), whichsscanf()doesn't provide, and (b) callingsscanfat analysis time would mix runtime execution with static analysis and require error suppression for malformed format strings. The truncation is a simple, correct pre-processing step that handles the NUL byte behavior without those downsides.