Skip to content

Commit 6e87485

Browse files
committed
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1: Fix GH-9883 SplFileObject::__toString() reads next line
2 parents f97f805 + 6fbf81c commit 6e87485

File tree

6 files changed

+58
-33
lines changed

6 files changed

+58
-33
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ PHP NEWS
1818
- Session:
1919
. Fixed GH-9932 (session name silently fails with . and [). (David Carlier)
2020

21+
- SPL:
22+
. Fixed GH-9883 (SplFileObject::__toString() reads next line). (Girgias)
23+
2124
27 Nov 2022, PHP 8.2.0
2225

2326
- CLI:

ext/spl/spl_directory.c

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,35 +1859,7 @@ zend_object_iterator *spl_filesystem_tree_get_iterator(zend_class_entry *ce, zva
18591859
}
18601860
/* }}} */
18611861

1862-
/* {{{ spl_filesystem_object_cast */
1863-
static zend_result spl_filesystem_object_cast(zend_object *readobj, zval *writeobj, int type)
1864-
{
1865-
spl_filesystem_object *intern = spl_filesystem_from_obj(readobj);
1866-
1867-
if (type == IS_STRING) {
1868-
if (readobj->ce->__tostring) {
1869-
return zend_std_cast_object_tostring(readobj, writeobj, type);
1870-
}
1871-
1872-
switch (intern->type) {
1873-
case SPL_FS_INFO:
1874-
case SPL_FS_FILE:
1875-
ZVAL_STR_COPY(writeobj, intern->file_name);
1876-
return SUCCESS;
1877-
case SPL_FS_DIR:
1878-
ZVAL_STRING(writeobj, intern->u.dir.entry.d_name);
1879-
return SUCCESS;
1880-
}
1881-
} else if (type == _IS_BOOL) {
1882-
ZVAL_TRUE(writeobj);
1883-
return SUCCESS;
1884-
}
1885-
ZVAL_NULL(writeobj);
1886-
return FAILURE;
1887-
}
1888-
/* }}} */
1889-
1890-
static zend_result spl_filesystem_file_read_ex(spl_filesystem_object *intern, bool silent, zend_long line_add, bool csv) /* {{{ */
1862+
static zend_result spl_filesystem_file_read_ex(spl_filesystem_object *intern, bool silent, zend_long line_add, bool csv)
18911863
{
18921864
char *buf;
18931865
size_t line_len = 0;
@@ -2751,6 +2723,23 @@ PHP_METHOD(SplFileObject, seek)
27512723
}
27522724
} /* }}} */
27532725

2726+
PHP_METHOD(SplFileObject, __toString)
2727+
{
2728+
if (zend_parse_parameters_none() == FAILURE) {
2729+
RETURN_THROWS();
2730+
}
2731+
2732+
spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2733+
2734+
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2735+
2736+
if (!intern->u.file.current_line && Z_ISUNDEF(intern->u.file.current_zval)) {
2737+
spl_filesystem_file_read_line(ZEND_THIS, intern);
2738+
}
2739+
2740+
RETURN_STRINGL(intern->u.file.current_line, intern->u.file.current_line_len);
2741+
}
2742+
27542743
/* {{{ PHP_MINIT_FUNCTION(spl_directory) */
27552744
PHP_MINIT_FUNCTION(spl_directory)
27562745
{
@@ -2760,7 +2749,6 @@ PHP_MINIT_FUNCTION(spl_directory)
27602749
memcpy(&spl_filesystem_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
27612750
spl_filesystem_object_handlers.offset = XtOffsetOf(spl_filesystem_object, std);
27622751
spl_filesystem_object_handlers.clone_obj = spl_filesystem_object_clone;
2763-
spl_filesystem_object_handlers.cast_object = spl_filesystem_object_cast;
27642752
spl_filesystem_object_handlers.dtor_obj = spl_filesystem_object_destroy_object;
27652753
spl_filesystem_object_handlers.free_obj = spl_filesystem_object_free_storage;
27662754

ext/spl/spl_directory.stub.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,6 @@ public function seek(int $line): void {}
378378
*/
379379
public function getCurrentLine(): string {}
380380

381-
/** @implementation-alias SplFileObject::fgets */
382381
public function __toString(): string {}
383382
}
384383

ext/spl/spl_directory_arginfo.h

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/spl/tests/gh9883-extra.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Bug GH-9883 (SplFileObject::__toString() reads next line)
3+
--FILE--
4+
<?php
5+
$file_stream = new SplTempFileObject();
6+
7+
echo $file_stream; // line 4
8+
echo $file_stream; // line 5
9+
echo $file_stream; // line 6
10+
echo $file_stream; // line 7
11+
echo $file_stream; // line 8
12+
echo $file_stream; // line 9
13+
?>
14+
--EXPECT--

ext/spl/tests/gh9883.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Bug GH-9883 (SplFileObject::__toString() reads next line)
3+
--FILE--
4+
<?php
5+
$file_stream = new SplFileObject(__FILE__, 'rb');
6+
7+
echo $file_stream; // line 4
8+
echo $file_stream; // line 5
9+
echo $file_stream; // line 6
10+
echo $file_stream; // line 7
11+
echo $file_stream; // line 8
12+
echo $file_stream; // line 9
13+
?>
14+
--EXPECT--
15+
<?php
16+
<?php
17+
<?php
18+
<?php
19+
<?php
20+
<?php

0 commit comments

Comments
 (0)