Skip to content

Fix GH-15653: fgetcsv overflow on length parameter. #15655

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
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ext/standard/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -1895,8 +1895,8 @@ PHP_FUNCTION(fgetcsv)

if (len_is_null || len == 0) {
len = -1;
} else if (len < 0) {
zend_argument_value_error(2, "must be a greater than or equal to 0");
} else if (len < 0 || len > (ZEND_LONG_MAX - 1)) {
zend_argument_value_error(2, "must be between 0 and " ZEND_LONG_FMT, (ZEND_LONG_MAX - 1));
RETURN_THROWS();
}

Expand Down
8 changes: 4 additions & 4 deletions ext/standard/tests/file/fgetcsv_error_conditions.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ try {
echo $e->getMessage() . \PHP_EOL;
}
?>
--EXPECT--
--EXPECTF--
fgetcsv() with negative length
fgetcsv(): Argument #2 ($length) must be a greater than or equal to 0
fgetcsv(): Argument #2 ($length) must be a greater than or equal to 0
fgetcsv(): Argument #2 ($length) must be a greater than or equal to 0
fgetcsv(): Argument #2 ($length) must be between 0 and %d
fgetcsv(): Argument #2 ($length) must be between 0 and %d
fgetcsv(): Argument #2 ($length) must be between 0 and %d
fgetcsv() with delimiter as empty string
fgetcsv(): Argument #3 ($separator) must be a single character
fgetcsv() with enclosure as empty string
Expand Down
22 changes: 22 additions & 0 deletions ext/standard/tests/file/gh15653.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
GH-15653 (fgetcsv overflow on length argument)
--FILE--
<?php
$filename = __DIR__ . "/gh15653.tmp";
touch($filename);
$fp = fopen ($filename, "r");

try {
fgetcsv($fp, PHP_INT_MAX);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be safe to also test for PHP_INT_MAX - 1.

} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}

fgetcsv($fp, PHP_INT_MAX-1);
--CLEAN--
<?php
@unlink(__DIR__ . "/gh15653.tmp");
?>
--EXPECTF--
fgetcsv(): Argument #2 ($length) must be between 0 and %d
%A
Loading