Skip to content

Commit 28baa7f

Browse files
committed
Fix GH-16477 (Segmentation fault when calling __debugInfo() after failed SplFileObject::__constructor)
1 parent 5ef3fe2 commit 28baa7f

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

ext/spl/spl_directory.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,13 +2047,7 @@ PHP_METHOD(SplFileObject, __construct)
20472047
size_t path_len;
20482048
zend_error_handling error_handling;
20492049

2050-
intern->u.file.open_mode = ZSTR_CHAR('r');
2051-
2052-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "P|Sbr!",
2053-
&intern->file_name, &open_mode,
2054-
&use_include_path, &intern->u.file.zcontext) == FAILURE) {
2055-
intern->u.file.open_mode = NULL;
2056-
intern->file_name = NULL;
2050+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "P|Sbr!", &intern->file_name, &open_mode, &use_include_path, &intern->u.file.zcontext) == FAILURE) {
20572051
RETURN_THROWS();
20582052
}
20592053

@@ -2096,6 +2090,12 @@ PHP_METHOD(SplTempFileObject, __construct)
20962090
RETURN_THROWS();
20972091
}
20982092

2093+
/* Prevent reinitialization of Object */
2094+
if (intern->u.file.stream) {
2095+
zend_throw_error(NULL, "cannot call constructor twice");
2096+
RETURN_THROWS();
2097+
}
2098+
20992099
if (max_memory < 0) {
21002100
file_name = zend_string_init("php://memory", sizeof("php://memory")-1, 0);
21012101
} else if (ZEND_NUM_ARGS()) {

ext/spl/tests/gh16477-2.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
GH-16477-2: Memory leak when calling SplTempFileObject::__constructor() twice
3+
--FILE--
4+
<?php
5+
6+
$obj = new SplTempFileObject();
7+
8+
try {
9+
$obj->__construct();
10+
} catch (Throwable $e) {
11+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
12+
}
13+
$obj->__debugInfo();
14+
15+
?>
16+
DONE
17+
--EXPECT--
18+
Error: cannot call constructor twice
19+
DONE

ext/spl/tests/gh16477.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
GH-16477: Segmentation fault when calling __debugInfo() after failed SplFileObject::__constructor
3+
--FILE--
4+
<?php
5+
6+
$obj = new SplFileObject(__FILE__);
7+
8+
try {
9+
$obj->__construct();
10+
} catch (Throwable $e) {
11+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
12+
}
13+
$obj->__debugInfo();
14+
15+
?>
16+
DONE
17+
--EXPECT--
18+
ArgumentCountError: SplFileObject::__construct() expects at least 1 argument, 0 given
19+
DONE

0 commit comments

Comments
 (0)