Skip to content

Commit 8a67dfd

Browse files
committed
Ensure correct return type from SplFileObject::getCurrentLine()
This is necessary to maintain return type consistency once tentative return types are added.
1 parent 188b1d4 commit 8a67dfd

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

ext/spl/spl_directory.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,26 +1948,29 @@ static int spl_filesystem_file_read_line_ex(zval * this_ptr, spl_filesystem_obje
19481948
}
19491949
if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV)) {
19501950
return spl_filesystem_file_read_csv(intern, intern->u.file.delimiter, intern->u.file.enclosure, intern->u.file.escape, NULL);
1951-
} else {
1952-
zend_execute_data *execute_data = EG(current_execute_data);
1953-
zend_call_method_with_0_params(Z_OBJ_P(this_ptr), Z_OBJCE_P(ZEND_THIS), &intern->u.file.func_getCurr, "getCurrentLine", &retval);
19541951
}
1955-
if (!Z_ISUNDEF(retval)) {
1956-
if (intern->u.file.current_line || !Z_ISUNDEF(intern->u.file.current_zval)) {
1957-
intern->u.file.current_line_num++;
1958-
}
1959-
spl_filesystem_file_free_line(intern);
1960-
if (Z_TYPE(retval) == IS_STRING) {
1961-
intern->u.file.current_line = estrndup(Z_STRVAL(retval), Z_STRLEN(retval));
1962-
intern->u.file.current_line_len = Z_STRLEN(retval);
1963-
} else {
1964-
ZVAL_COPY_DEREF(&intern->u.file.current_zval, &retval);
1965-
}
1952+
1953+
zend_execute_data *execute_data = EG(current_execute_data);
1954+
zend_call_method_with_0_params(Z_OBJ_P(this_ptr), Z_OBJCE_P(ZEND_THIS), &intern->u.file.func_getCurr, "getCurrentLine", &retval);
1955+
if (Z_ISUNDEF(retval)) {
1956+
return FAILURE;
1957+
}
1958+
1959+
if (Z_TYPE(retval) != IS_STRING) {
1960+
zend_type_error("getCurrentLine(): Return value must be of type string, %s returned",
1961+
zend_zval_type_name(&retval));
19661962
zval_ptr_dtor(&retval);
1967-
return SUCCESS;
1968-
} else {
19691963
return FAILURE;
19701964
}
1965+
1966+
if (intern->u.file.current_line || !Z_ISUNDEF(intern->u.file.current_zval)) {
1967+
intern->u.file.current_line_num++;
1968+
}
1969+
spl_filesystem_file_free_line(intern);
1970+
intern->u.file.current_line = estrndup(Z_STRVAL(retval), Z_STRLEN(retval));
1971+
intern->u.file.current_line_len = Z_STRLEN(retval);
1972+
zval_ptr_dtor(&retval);
1973+
return SUCCESS;
19711974
} else {
19721975
return spl_filesystem_file_read(intern, silent);
19731976
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Invalid SplFileObject::getCurrentLine() return type
3+
--FILE--
4+
<?php
5+
6+
class MySplFileObject extends SplFileObject {
7+
public function getCurrentLine(): array {
8+
return [1, 2, 3];
9+
}
10+
}
11+
12+
$obj = new MySplFileObject(__FILE__);
13+
try {
14+
var_dump($obj->current());
15+
} catch (TypeError $e) {
16+
echo $e->getMessage(), "\n";
17+
}
18+
19+
?>
20+
--EXPECT--
21+
getCurrentLine(): Return value must be of type string, array returned

0 commit comments

Comments
 (0)