From 9c28c83dd6880eb991a273d771c4eb01cb00ec80 Mon Sep 17 00:00:00 2001 From: Aliaksandr Bystry Date: Sun, 28 Nov 2021 08:52:23 +0100 Subject: [PATCH 1/2] Fix #75917: SplFileObject::seek broken with CSV flags. --- ext/spl/spl_directory.c | 2 +- ext/spl/tests/bug75917.phpt | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 ext/spl/tests/bug75917.phpt diff --git a/ext/spl/spl_directory.c b/ext/spl/spl_directory.c index c7dd8b472173f..9ee1b08c13cab 100644 --- a/ext/spl/spl_directory.c +++ b/ext/spl/spl_directory.c @@ -2732,7 +2732,7 @@ PHP_METHOD(SplFileObject, seek) spl_filesystem_file_rewind(ZEND_THIS, intern); for (i = 0; i < line_pos; i++) { - if (spl_filesystem_file_read_line(ZEND_THIS, intern, 1) == FAILURE) { + if (spl_filesystem_file_read_line(ZEND_THIS, intern, 1) == FAILURE && !intern->flags) { return; } } diff --git a/ext/spl/tests/bug75917.phpt b/ext/spl/tests/bug75917.phpt new file mode 100644 index 0000000000000..6a318bc68159d --- /dev/null +++ b/ext/spl/tests/bug75917.phpt @@ -0,0 +1,24 @@ +--TEST-- +Bug #75917 SplFileObject::seek broken with CSV flags +--FILE-- +fputcsv($row); +} +$tmp->setFlags(0); +$tmp->seek(23); +var_dump($tmp->current()); + +$tmp->setFlags(SplFileObject::READ_CSV | SplFileObject::SKIP_EMPTY); +$tmp->seek(23); +var_dump($tmp->current()); +?> +--EXPECT-- +bool(false) +bool(false) From 776ece2f3e3714d255b90926171bc1c90a77f4d8 Mon Sep 17 00:00:00 2001 From: Aliaksandr Bystry Date: Tue, 30 Nov 2021 14:23:50 +0100 Subject: [PATCH 2/2] Free file line for when SPL_FILE_OBJECT_READ_CSV is set. --- ext/spl/spl_directory.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ext/spl/spl_directory.c b/ext/spl/spl_directory.c index 9ee1b08c13cab..045aad5bc1b02 100644 --- a/ext/spl/spl_directory.c +++ b/ext/spl/spl_directory.c @@ -1939,6 +1939,8 @@ static int spl_filesystem_file_read_line_ex(zval * this_ptr, spl_filesystem_obje /* 1) use fgetcsv? 2) overloaded call the function, 3) do it directly */ if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV) || intern->u.file.func_getCurr->common.scope != spl_ce_SplFileObject) { + spl_filesystem_file_free_line(intern); + if (php_stream_eof(intern->u.file.stream)) { if (!silent) { zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Cannot read from file %s", intern->file_name); @@ -2732,7 +2734,7 @@ PHP_METHOD(SplFileObject, seek) spl_filesystem_file_rewind(ZEND_THIS, intern); for (i = 0; i < line_pos; i++) { - if (spl_filesystem_file_read_line(ZEND_THIS, intern, 1) == FAILURE && !intern->flags) { + if (spl_filesystem_file_read_line(ZEND_THIS, intern, 1) == FAILURE) { return; } }