Skip to content
Open
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: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ PHP NEWS
of the curl_copy_handle() function to clone a CurlHandle. (timwolla)
. Fix curl build failure on macOS+curl 8.16. (nielsdos)

- SPL:
. Fixed bug GH-19942 (iterator_count() on an empty SplFileObject looping
infinitely). (alexandre-daubois)

- Soap:
. Fixed bug GH-19784 (SoapServer memory leak). (nielsdos)

Expand Down
23 changes: 23 additions & 0 deletions ext/spl/spl_directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -2166,6 +2166,29 @@ PHP_METHOD(SplFileObject, valid)
if (!intern->u.file.stream) {
RETURN_FALSE;
}

/* For empty files, php_stream_eof() might not return TRUE until after a read attempt.
* If we're at the beginning and haven't read anything, check EOF directly after ensuring
* a read has been attempted (only on seekable streams). */
if (!intern->u.file.current_line && Z_ISUNDEF(intern->u.file.current_zval) &&
intern->u.file.current_line_num == 0) {

if (intern->u.file.stream->ops->seek &&
(intern->u.file.stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) {

if (php_stream_eof(intern->u.file.stream)) {
RETURN_FALSE;
}

if (php_stream_tell(intern->u.file.stream) == 0) {
php_stream_statbuf ssb;
if (php_stream_stat(intern->u.file.stream, &ssb) == 0 && ssb.sb.st_size == 0) {
RETURN_FALSE;
}
}
}
}

RETURN_BOOL(!php_stream_eof(intern->u.file.stream));
} /* }}} */

Expand Down
16 changes: 16 additions & 0 deletions ext/spl/tests/SplFileObject_iterator_count_empty_file.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
GH-19942 (iterator_count() on an empty SplFileObject should return 0)
--FILE--
<?php
$tmpfile = tempnam(sys_get_temp_dir(), 'spl_empty_test');
touch($tmpfile);

$fileObject = new SplFileObject($tmpfile, 'r');
$count = iterator_count($fileObject);

var_dump($count);

unlink($tmpfile);
?>
--EXPECT--
int(0)
11 changes: 11 additions & 0 deletions ext/spl/tests/SplFileObject_non_seekable_stream.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
SplFileObject::valid() with non-seekable streams should not hang
--FILE--
<?php
$file = new SplFileObject("php://stdin", "r");
var_dump($file->valid());
?>
--STDIN--

--EXPECT--
bool(true)
Loading