Skip to content

Commit 60d8e92

Browse files
committed
Guard uninitialized SplFileObject in fputcsv() and next()
fputcsv() and next() reached the file stream without the CHECK_SPL_FILE_OBJECT_IS_INITIALIZED guard their siblings carry, so invoking them via reflection on an object from newInstanceWithoutConstructor() (constructor bypassed, stream NULL) crashed instead of throwing. next() only touches the stream with the READ_AHEAD flag, itself settable through the equally unguarded setFlags(). Both now throw "Object not initialized". Fixes GH-16217 Closes GH-22454
1 parent f5c4cae commit 60d8e92

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ PHP NEWS
2424
(jorgsowa)
2525
. Ignore leading back-slash in class_parents(), class_implements(), and
2626
class_uses(). (jorgsowa)
27+
. Fixed bug GH-16217 (SplFileObject::fputcsv() on an uninitialized object
28+
segfaults). (iliaal)
2729

2830
- Standard:
2931
. Fixed bug GH-22360 (convert.base64-encode corruption on

‎ext/spl/spl_directory.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,6 +2224,8 @@ PHP_METHOD(SplFileObject, next)
22242224
RETURN_THROWS();
22252225
}
22262226

2227+
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2228+
22272229
spl_filesystem_file_free_line(intern);
22282230
if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_AHEAD)) {
22292231
spl_filesystem_file_read_line(ZEND_THIS, intern, true);
@@ -2376,6 +2378,8 @@ PHP_METHOD(SplFileObject, fputcsv)
23762378
RETURN_THROWS();
23772379
}
23782380

2381+
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2382+
23792383
if (delim) {
23802384
if (d_len != 1) {
23812385
zend_argument_value_error(2, "must be a single character");

‎ext/spl/tests/gh16217.phpt‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
GH-16217 (SplFileObject methods on an uninitialized object segfault)
3+
--FILE--
4+
<?php
5+
function uninitialized(): SplFileObject {
6+
return (new ReflectionClass(SplFileObject::class))->newInstanceWithoutConstructor();
7+
}
8+
9+
try {
10+
(new ReflectionMethod(SplFileObject::class, "fputcsv"))->invoke(uninitialized(), []);
11+
} catch (Error $e) {
12+
echo "fputcsv: ", $e->getMessage(), "\n";
13+
}
14+
15+
try {
16+
(new ReflectionMethod(SplFileObject::class, "next"))->invoke(uninitialized());
17+
} catch (Error $e) {
18+
echo "next: ", $e->getMessage(), "\n";
19+
}
20+
21+
$obj = uninitialized();
22+
(new ReflectionMethod(SplFileObject::class, "setFlags"))->invoke($obj, SplFileObject::READ_AHEAD);
23+
try {
24+
(new ReflectionMethod(SplFileObject::class, "next"))->invoke($obj);
25+
} catch (Error $e) {
26+
echo "next (READ_AHEAD): ", $e->getMessage(), "\n";
27+
}
28+
29+
echo "Done\n";
30+
?>
31+
--EXPECT--
32+
fputcsv: Object not initialized
33+
next: Object not initialized
34+
next (READ_AHEAD): Object not initialized
35+
Done

0 commit comments

Comments
 (0)