-
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1104,6 +1104,10 @@ PHPAPI PHP_FUNCTION(fseek) | |
| Z_PARAM_LONG(whence) | ||
| ZEND_PARSE_PARAMETERS_END(); | ||
|
|
||
| if (whence < INT_MIN || whence > INT_MAX) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can just use ZEND_LONG_EXCEEDS_INT macro here. |
||
| RETURN_LONG(-1); | ||
| } | ||
|
|
||
| php_stream_error_operation_begin(); | ||
| RETVAL_LONG(php_stream_seek(stream, offset, (int) whence)); | ||
| php_stream_error_operation_end_for_stream(stream); | ||
|
|
||
| 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would also test for non file based streams, eg. memory. |
||
| --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" | ||
| 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'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also you need a CLEAN section for this. |
||
| 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) | ||
Uh oh!
There was an error while loading. Please reload this page.