1
0
mirror of https://github.com/php/php-src.git synced 2026-03-26 01:02:25 +01:00

Ensure correct return type from SplFileObject::getCurrentLine()

This is necessary to maintain return type consistency once
tentative return types are added.
This commit is contained in:
Nikita Popov
2021-07-06 14:32:39 +02:00
parent 188b1d4c7c
commit 8a67dfd16b
2 changed files with 41 additions and 17 deletions

View File

@@ -1948,26 +1948,29 @@ static int spl_filesystem_file_read_line_ex(zval * this_ptr, spl_filesystem_obje
}
if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV)) {
return spl_filesystem_file_read_csv(intern, intern->u.file.delimiter, intern->u.file.enclosure, intern->u.file.escape, NULL);
} else {
zend_execute_data *execute_data = EG(current_execute_data);
zend_call_method_with_0_params(Z_OBJ_P(this_ptr), Z_OBJCE_P(ZEND_THIS), &intern->u.file.func_getCurr, "getCurrentLine", &retval);
}
if (!Z_ISUNDEF(retval)) {
if (intern->u.file.current_line || !Z_ISUNDEF(intern->u.file.current_zval)) {
intern->u.file.current_line_num++;
}
spl_filesystem_file_free_line(intern);
if (Z_TYPE(retval) == IS_STRING) {
intern->u.file.current_line = estrndup(Z_STRVAL(retval), Z_STRLEN(retval));
intern->u.file.current_line_len = Z_STRLEN(retval);
} else {
ZVAL_COPY_DEREF(&intern->u.file.current_zval, &retval);
}
zval_ptr_dtor(&retval);
return SUCCESS;
} else {
zend_execute_data *execute_data = EG(current_execute_data);
zend_call_method_with_0_params(Z_OBJ_P(this_ptr), Z_OBJCE_P(ZEND_THIS), &intern->u.file.func_getCurr, "getCurrentLine", &retval);
if (Z_ISUNDEF(retval)) {
return FAILURE;
}
if (Z_TYPE(retval) != IS_STRING) {
zend_type_error("getCurrentLine(): Return value must be of type string, %s returned",
zend_zval_type_name(&retval));
zval_ptr_dtor(&retval);
return FAILURE;
}
if (intern->u.file.current_line || !Z_ISUNDEF(intern->u.file.current_zval)) {
intern->u.file.current_line_num++;
}
spl_filesystem_file_free_line(intern);
intern->u.file.current_line = estrndup(Z_STRVAL(retval), Z_STRLEN(retval));
intern->u.file.current_line_len = Z_STRLEN(retval);
zval_ptr_dtor(&retval);
return SUCCESS;
} else {
return spl_filesystem_file_read(intern, silent);
}

View File

@@ -0,0 +1,21 @@
--TEST--
Invalid SplFileObject::getCurrentLine() return type
--FILE--
<?php
class MySplFileObject extends SplFileObject {
public function getCurrentLine(): array {
return [1, 2, 3];
}
}
$obj = new MySplFileObject(__FILE__);
try {
var_dump($obj->current());
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
?>
--EXPECT--
getCurrentLine(): Return value must be of type string, array returned