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 @@ -57,6 +57,10 @@ PHP NEWS
. Fixed bug GH-19988 (zend_string_init with NULL pointer in simplexml (UB)).
(nielsdos)

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

- Soap:
. Fixed bug GH-19784 (SoapServer memory leak). (nielsdos)
. Fixed bug GH-20011 (Array of SoapVar of unknown type causes crash).
Expand Down
8 changes: 8 additions & 0 deletions ext/spl/spl_directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -2235,9 +2235,17 @@ PHP_METHOD(SplFileObject, next)
RETURN_THROWS();
}

bool had_current_line = (intern->u.file.current_line != NULL || !Z_ISUNDEF(intern->u.file.current_zval));
spl_filesystem_file_free_line(intern);
if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_AHEAD)) {
spl_filesystem_file_read_line(ZEND_THIS, intern, true);
} else if (!had_current_line) {
/* If there was no current line before, we need to advance the stream position
* for iterator_count() and other iterator operations to work correctly.
* Read and immediately discard a line. */
if (spl_filesystem_file_read_line(ZEND_THIS, intern, true) == SUCCESS) {
spl_filesystem_file_free_line(intern);
}
}
intern->u.file.current_line_num++;
} /* }}} */
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(1)
16 changes: 16 additions & 0 deletions ext/spl/tests/SplFileObject_iterator_count_non_empty_file.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
GH-19942 (iterator_count() on a non-empty SplFileObject should return correct count)
--FILE--
<?php
$tmpfile = tempnam(sys_get_temp_dir(), 'spl_non_empty_test');
file_put_contents($tmpfile, "line1\nline2\nline3");

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

var_dump($count);

unlink($tmpfile);
?>
--EXPECT--
int(3)
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