Skip to content

Commit 295d4ae

Browse files
committed
Refactor code handling file.current_zval
The Zval is always an array
1 parent ef07676 commit 295d4ae

File tree

2 files changed

+57
-26
lines changed

2 files changed

+57
-26
lines changed

ext/spl/spl_directory.c

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,6 +1959,9 @@ static zend_result spl_filesystem_file_read_csv(spl_filesystem_object *intern, c
19591959
}
19601960

19611961
php_fgetcsv(intern->u.file.stream, delimiter, enclosure, escape, buf_len, buf, &intern->u.file.current_zval);
1962+
/* php_fgetcsv() only ever sets an array */
1963+
ZEND_ASSERT(Z_TYPE(intern->u.file.current_zval) == IS_ARRAY);
1964+
19621965
if (return_value) {
19631966
ZVAL_COPY(return_value, &intern->u.file.current_zval);
19641967
}
@@ -2011,34 +2014,30 @@ static bool spl_filesystem_file_is_empty_line(spl_filesystem_object *intern) /*
20112014
if (intern->u.file.current_line) {
20122015
return intern->u.file.current_line_len == 0;
20132016
} else if (!Z_ISUNDEF(intern->u.file.current_zval)) {
2014-
switch(Z_TYPE(intern->u.file.current_zval)) {
2015-
case IS_STRING:
2016-
return Z_STRLEN(intern->u.file.current_zval) == 0;
2017-
case IS_ARRAY:
2018-
if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV)
2019-
&& zend_hash_num_elements(Z_ARRVAL(intern->u.file.current_zval)) == 1) {
2020-
uint32_t idx = 0;
2021-
zval *first;
2022-
2023-
if (HT_IS_PACKED(Z_ARRVAL(intern->u.file.current_zval))) {
2024-
while (Z_ISUNDEF(Z_ARRVAL(intern->u.file.current_zval)->arPacked[idx])) {
2025-
idx++;
2026-
}
2027-
first = &Z_ARRVAL(intern->u.file.current_zval)->arPacked[idx];
2028-
} else {
2029-
while (Z_ISUNDEF(Z_ARRVAL(intern->u.file.current_zval)->arData[idx].val)) {
2030-
idx++;
2031-
}
2032-
first = &Z_ARRVAL(intern->u.file.current_zval)->arData[idx].val;
2033-
}
2034-
return Z_TYPE_P(first) == IS_STRING && Z_STRLEN_P(first) == 0;
2017+
ZEND_ASSERT(Z_TYPE(intern->u.file.current_zval) == IS_ARRAY);
2018+
/* TODO Figure out when this branch can happen... */
2019+
if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV)
2020+
&& zend_hash_num_elements(Z_ARRVAL(intern->u.file.current_zval)) == 1) {
2021+
ZEND_ASSERT(false && "Can this happen?");
2022+
uint32_t idx = 0;
2023+
zval *first;
2024+
2025+
if (HT_IS_PACKED(Z_ARRVAL(intern->u.file.current_zval))) {
2026+
while (Z_ISUNDEF(Z_ARRVAL(intern->u.file.current_zval)->arPacked[idx])) {
2027+
idx++;
2028+
}
2029+
first = &Z_ARRVAL(intern->u.file.current_zval)->arPacked[idx];
2030+
ZEND_ASSERT(Z_TYPE_P(first) == IS_STRING);
2031+
} else {
2032+
while (Z_ISUNDEF(Z_ARRVAL(intern->u.file.current_zval)->arData[idx].val)) {
2033+
idx++;
20352034
}
2036-
return zend_hash_num_elements(Z_ARRVAL(intern->u.file.current_zval)) == 0;
2037-
case IS_NULL:
2038-
return 1;
2039-
default:
2040-
return 0;
2035+
first = &Z_ARRVAL(intern->u.file.current_zval)->arData[idx].val;
2036+
ZEND_ASSERT(Z_TYPE_P(first) == IS_STRING);
2037+
}
2038+
return Z_STRLEN_P(first) == 0;
20412039
}
2040+
return zend_hash_num_elements(Z_ARRVAL(intern->u.file.current_zval)) == 0;
20422041
} else {
20432042
return 1;
20442043
}
@@ -2234,6 +2233,7 @@ PHP_METHOD(SplFileObject, current)
22342233
RETURN_STRINGL(intern->u.file.current_line, intern->u.file.current_line_len);
22352234
} else if (!Z_ISUNDEF(intern->u.file.current_zval)) {
22362235
ZEND_ASSERT(!Z_ISREF(intern->u.file.current_zval));
2236+
ZEND_ASSERT(Z_TYPE(intern->u.file.current_zval) == IS_ARRAY);
22372237
RETURN_COPY(&intern->u.file.current_zval);
22382238
}
22392239
RETURN_FALSE;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
SplFileObject: fgetcsv() on a blank line
3+
--FILE--
4+
<?php
5+
6+
$file_path = __DIR__ . '/SplFileObject_fgetcsv_empty.csv';
7+
$file = new SplFileObject($file_path, 'w');
8+
$file = new SplTempFileObject();
9+
10+
// write to file
11+
$file->fwrite("");
12+
13+
// read from file
14+
$file->rewind();
15+
var_dump($file->fgetcsv());
16+
17+
$file->setFlags(SplFileObject::SKIP_EMPTY);
18+
$file->rewind();
19+
var_dump($file->fgetcsv());
20+
?>
21+
--CLEAN--
22+
<?php
23+
$file_path = __DIR__ . '/SplFileObject_fgetcsv_empty.csv';
24+
unlink($file_path);
25+
?>
26+
--EXPECT--
27+
array(1) {
28+
[0]=>
29+
NULL
30+
}
31+
bool(false)

0 commit comments

Comments
 (0)