Skip to content

Commit b97d2b6

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

File tree

5 files changed

+55
-33
lines changed

5 files changed

+55
-33
lines changed

ext/spl/spl_directory.c

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,35 +1849,7 @@ zend_object_iterator *spl_filesystem_tree_get_iterator(zend_class_entry *ce, zva
18491849
}
18501850
/* }}} */
18511851

1852-
/* {{{ spl_filesystem_object_cast */
1853-
static zend_result spl_filesystem_object_cast(zend_object *readobj, zval *writeobj, int type)
1854-
{
1855-
spl_filesystem_object *intern = spl_filesystem_from_obj(readobj);
1856-
1857-
if (type == IS_STRING) {
1858-
if (readobj->ce->__tostring) {
1859-
return zend_std_cast_object_tostring(readobj, writeobj, type);
1860-
}
1861-
1862-
switch (intern->type) {
1863-
case SPL_FS_INFO:
1864-
case SPL_FS_FILE:
1865-
ZVAL_STR_COPY(writeobj, intern->file_name);
1866-
return SUCCESS;
1867-
case SPL_FS_DIR:
1868-
ZVAL_STRING(writeobj, intern->u.dir.entry.d_name);
1869-
return SUCCESS;
1870-
}
1871-
} else if (type == _IS_BOOL) {
1872-
ZVAL_TRUE(writeobj);
1873-
return SUCCESS;
1874-
}
1875-
ZVAL_NULL(writeobj);
1876-
return FAILURE;
1877-
}
1878-
/* }}} */
1879-
1880-
static zend_result spl_filesystem_file_read_ex(spl_filesystem_object *intern, bool silent, zend_long line_add, bool csv) /* {{{ */
1852+
static zend_result spl_filesystem_file_read_ex(spl_filesystem_object *intern, bool silent, zend_long line_add, bool csv)
18811853
{
18821854
char *buf;
18831855
size_t line_len = 0;
@@ -2741,6 +2713,23 @@ PHP_METHOD(SplFileObject, seek)
27412713
}
27422714
} /* }}} */
27432715

2716+
PHP_METHOD(SplFileObject, __toString)
2717+
{
2718+
if (zend_parse_parameters_none() == FAILURE) {
2719+
RETURN_THROWS();
2720+
}
2721+
2722+
spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2723+
2724+
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2725+
2726+
if (!intern->u.file.current_line && Z_ISUNDEF(intern->u.file.current_zval)) {
2727+
spl_filesystem_file_read_line(ZEND_THIS, intern);
2728+
}
2729+
2730+
RETURN_STRINGL(intern->u.file.current_line, intern->u.file.current_line_len);
2731+
}
2732+
27442733
/* {{{ PHP_MINIT_FUNCTION(spl_directory) */
27452734
PHP_MINIT_FUNCTION(spl_directory)
27462735
{
@@ -2751,7 +2740,6 @@ PHP_MINIT_FUNCTION(spl_directory)
27512740
memcpy(&spl_filesystem_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
27522741
spl_filesystem_object_handlers.offset = XtOffsetOf(spl_filesystem_object, std);
27532742
spl_filesystem_object_handlers.clone_obj = spl_filesystem_object_clone;
2754-
spl_filesystem_object_handlers.cast_object = spl_filesystem_object_cast;
27552743
spl_filesystem_object_handlers.dtor_obj = spl_filesystem_object_destroy_object;
27562744
spl_filesystem_object_handlers.free_obj = spl_filesystem_object_free_storage;
27572745

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)