Skip to content
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
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ PHP NEWS
(jorgsowa)
. Ignore leading back-slash in class_parents(), class_implements(), and
class_uses(). (jorgsowa)
. Fixed bug GH-16217 (SplFileObject::fputcsv() on an uninitialized object
segfaults). (iliaal)

- Standard:
. Fixed bug GH-22360 (convert.base64-encode corruption on
Expand Down
4 changes: 4 additions & 0 deletions ext/spl/spl_directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -2224,6 +2224,8 @@ PHP_METHOD(SplFileObject, next)
RETURN_THROWS();
}

CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);

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);
Expand Down Expand Up @@ -2376,6 +2378,8 @@ PHP_METHOD(SplFileObject, fputcsv)
RETURN_THROWS();
}

CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);

if (delim) {
if (d_len != 1) {
zend_argument_value_error(2, "must be a single character");
Expand Down
35 changes: 35 additions & 0 deletions ext/spl/tests/gh16217.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
GH-16217 (SplFileObject methods on an uninitialized object segfault)
--FILE--
<?php
function uninitialized(): SplFileObject {
return (new ReflectionClass(SplFileObject::class))->newInstanceWithoutConstructor();
}

try {
(new ReflectionMethod(SplFileObject::class, "fputcsv"))->invoke(uninitialized(), []);
} catch (Error $e) {
echo "fputcsv: ", $e->getMessage(), "\n";
}

try {
(new ReflectionMethod(SplFileObject::class, "next"))->invoke(uninitialized());
} catch (Error $e) {
echo "next: ", $e->getMessage(), "\n";
}

$obj = uninitialized();
(new ReflectionMethod(SplFileObject::class, "setFlags"))->invoke($obj, SplFileObject::READ_AHEAD);
try {
(new ReflectionMethod(SplFileObject::class, "next"))->invoke($obj);
} catch (Error $e) {
echo "next (READ_AHEAD): ", $e->getMessage(), "\n";
}

echo "Done\n";
?>
--EXPECT--
fputcsv: Object not initialized
next: Object not initialized
next (READ_AHEAD): Object not initialized
Done
Loading