Skip to content

Refactor code handling file.current_zval #8934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 23 additions & 26 deletions ext/spl/spl_directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -2025,34 +2025,30 @@ static bool spl_filesystem_file_is_empty_line(spl_filesystem_object *intern) /*
if (intern->u.file.current_line) {
return intern->u.file.current_line_len == 0;
} else if (!Z_ISUNDEF(intern->u.file.current_zval)) {
switch(Z_TYPE(intern->u.file.current_zval)) {
case IS_STRING:
return Z_STRLEN(intern->u.file.current_zval) == 0;
case IS_ARRAY:
if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV)
&& zend_hash_num_elements(Z_ARRVAL(intern->u.file.current_zval)) == 1) {
uint32_t idx = 0;
zval *first;

if (HT_IS_PACKED(Z_ARRVAL(intern->u.file.current_zval))) {
while (Z_ISUNDEF(Z_ARRVAL(intern->u.file.current_zval)->arPacked[idx])) {
idx++;
}
first = &Z_ARRVAL(intern->u.file.current_zval)->arPacked[idx];
} else {
while (Z_ISUNDEF(Z_ARRVAL(intern->u.file.current_zval)->arData[idx].val)) {
idx++;
}
first = &Z_ARRVAL(intern->u.file.current_zval)->arData[idx].val;
}
return Z_TYPE_P(first) == IS_STRING && Z_STRLEN_P(first) == 0;
ZEND_ASSERT(Z_TYPE(intern->u.file.current_zval) == IS_ARRAY);
/* TODO Figure out when this branch can happen... */
if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV)
&& zend_hash_num_elements(Z_ARRVAL(intern->u.file.current_zval)) == 1) {
ZEND_ASSERT(false && "Can this happen?");
uint32_t idx = 0;
zval *first;

if (HT_IS_PACKED(Z_ARRVAL(intern->u.file.current_zval))) {
while (Z_ISUNDEF(Z_ARRVAL(intern->u.file.current_zval)->arPacked[idx])) {
idx++;
}
return zend_hash_num_elements(Z_ARRVAL(intern->u.file.current_zval)) == 0;
case IS_NULL:
return 1;
default:
return 0;
first = &Z_ARRVAL(intern->u.file.current_zval)->arPacked[idx];
ZEND_ASSERT(Z_TYPE_P(first) == IS_STRING);
} else {
while (Z_ISUNDEF(Z_ARRVAL(intern->u.file.current_zval)->arData[idx].val)) {
idx++;
}
first = &Z_ARRVAL(intern->u.file.current_zval)->arData[idx].val;
ZEND_ASSERT(Z_TYPE_P(first) == IS_STRING);
}
return Z_STRLEN_P(first) == 0;
}
return zend_hash_num_elements(Z_ARRVAL(intern->u.file.current_zval)) == 0;
} else {
return 1;
}
Expand Down Expand Up @@ -2248,6 +2244,7 @@ PHP_METHOD(SplFileObject, current)
RETURN_STRINGL(intern->u.file.current_line, intern->u.file.current_line_len);
} else if (!Z_ISUNDEF(intern->u.file.current_zval)) {
ZEND_ASSERT(!Z_ISREF(intern->u.file.current_zval));
ZEND_ASSERT(Z_TYPE(intern->u.file.current_zval) == IS_ARRAY);
RETURN_COPY(&intern->u.file.current_zval);
}
RETURN_FALSE;
Expand Down
31 changes: 31 additions & 0 deletions ext/spl/tests/SplFileObject/fgetcsv_blank_file.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
SplFileObject: fgetcsv() on a blank line
--FILE--
<?php

$file_path = __DIR__ . '/SplFileObject_fgetcsv_empty.csv';
$file = new SplFileObject($file_path, 'w');
$file = new SplTempFileObject();

// write to file
$file->fwrite("");

// read from file
$file->rewind();
var_dump($file->fgetcsv());

$file->setFlags(SplFileObject::SKIP_EMPTY);
$file->rewind();
var_dump($file->fgetcsv());
?>
--CLEAN--
<?php
$file_path = __DIR__ . '/SplFileObject_fgetcsv_empty.csv';
unlink($file_path);
?>
--EXPECT--
array(1) {
[0]=>
NULL
}
bool(false)