-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fix fseek() accepting $whence values that do not fit in an int #23483
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
Open
lacatoire
wants to merge
2
commits into
php:master
Choose a base branch
from
lacatoire:fix/fseek-whence-int-narrowing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,40 @@ | ||
| --TEST-- | ||
| fseek(): an invalid $whence that fits in an int must not desynchronize the stream | ||
| --FILE-- | ||
| <?php | ||
| $tmp = __DIR__ . '/fseek_whence_invalid_inrange.tmp'; | ||
| file_put_contents($tmp, "0123456789"); | ||
|
|
||
| foreach ([99, -2147483648, 2147483647] as $whence) { | ||
| echo 'whence=', $whence, PHP_EOL; | ||
| $h = fopen($tmp, 'r'); | ||
| var_dump(fread($h, 4)); | ||
| var_dump(fseek($h, 3, $whence)); | ||
| var_dump(ftell($h)); | ||
| var_dump(fread($h, 6)); | ||
| fclose($h); | ||
| echo PHP_EOL; | ||
| } | ||
| ?> | ||
| --CLEAN-- | ||
| <?php | ||
| @unlink(__DIR__ . '/fseek_whence_invalid_inrange.tmp'); | ||
| ?> | ||
| --EXPECT-- | ||
| whence=99 | ||
| string(4) "0123" | ||
| int(-1) | ||
| int(4) | ||
| string(6) "456789" | ||
|
|
||
| whence=-2147483648 | ||
| string(4) "0123" | ||
| int(-1) | ||
| int(4) | ||
| string(6) "456789" | ||
|
|
||
| whence=2147483647 | ||
| string(4) "0123" | ||
| int(-1) | ||
| int(4) | ||
| string(6) "456789" |
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,46 @@ | ||
| --TEST-- | ||
| fseek(): $whence values that overflow int must return -1, not alias onto a valid constant | ||
| --SKIPIF-- | ||
| <?php | ||
| if (PHP_INT_SIZE < 8) die("skip 64-bit only"); | ||
| ?> | ||
| --FILE-- | ||
| <?php | ||
| $tmp = tempnam(sys_get_temp_dir(), 'fseek'); | ||
| file_put_contents($tmp, "0123456789"); | ||
| $bias = 2 ** 32; | ||
|
|
||
| $h = fopen($tmp, 'r'); | ||
|
|
||
| // SEEK_CUR + 2**32 must not alias onto SEEK_CUR (1) | ||
| fseek($h, 4); | ||
| var_dump(fseek($h, 3, SEEK_CUR + $bias)); // -1 | ||
| var_dump(ftell($h)); // 4 (unchanged) | ||
|
|
||
| // SEEK_END + 2**32 must not alias onto SEEK_END (2) | ||
| fseek($h, 4); | ||
| var_dump(fseek($h, 3, SEEK_END + $bias)); // -1 | ||
| var_dump(ftell($h)); // 4 (unchanged) | ||
|
|
||
| // PHP_INT_MIN must not alias onto SEEK_SET (0) | ||
| fseek($h, 4); | ||
| var_dump(fseek($h, 3, PHP_INT_MIN)); // -1 | ||
| var_dump(ftell($h)); // 4 (unchanged) | ||
|
|
||
| // Sanity: normal SEEK_CUR still works | ||
| fseek($h, 4); | ||
| var_dump(fseek($h, 3, SEEK_CUR)); // 0 | ||
| var_dump(ftell($h)); // 7 | ||
|
|
||
| fclose($h); | ||
| unlink($tmp); | ||
| ?> | ||
| --EXPECT-- | ||
| int(-1) | ||
| int(4) | ||
| int(-1) | ||
| int(4) | ||
| int(-1) | ||
| int(4) | ||
| int(0) | ||
| int(7) |
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
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.
Uh oh!
There was an error while loading. Please reload this page.