Skip to content

Commit 52e7e0f

Browse files
Aliaksandr Bystrynikic
Aliaksandr Bystry
authored andcommitted
Fix bug #66588: SplFileObject::fgetcsv incorrectly returns a row on premature EOF
Make sure the behavior is the same regardless of whether there is a trailing newline and whether the input is a stream or not. Closes GH-7539.
1 parent 304d78b commit 52e7e0f

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ PHP NEWS
1010
- Standard:
1111
. Fixed bug #81491 (Incorrectly using libsodium for argon2 hashing).
1212
(Dan Pock)
13+
. Fixed bug #66588 (SplFileObject::fgetcsv incorrectly returns a row on
14+
premature EOF). (Aliaksandr Bystry)
1315

1416
- Streams:
1517
. Fixed bug #81475 (stream_isatty emits warning with attached stream wrapper).

ext/standard/file.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,12 +2144,7 @@ PHPAPI void php_fgetcsv(php_stream *stream, char delimiter, char enclosure, int
21442144
* assign all the data from the start of
21452145
* the enclosure to end of data to the
21462146
* last element */
2147-
if ((size_t)temp_len > (size_t)(limit - buf)) {
2148-
goto quit_loop_2;
2149-
}
2150-
zend_array_destroy(Z_ARR_P(return_value));
2151-
RETVAL_FALSE;
2152-
goto out;
2147+
goto quit_loop_2;
21532148
}
21542149

21552150
temp_len += new_len;
@@ -2300,7 +2295,6 @@ PHPAPI void php_fgetcsv(php_stream *stream, char delimiter, char enclosure, int
23002295
add_next_index_stringl(return_value, temp, comp_end - temp);
23012296
} while (inc_len > 0);
23022297

2303-
out:
23042298
efree(temp);
23052299
if (stream) {
23062300
efree(buf);

ext/standard/tests/file/bug66588.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Bug #66588 SplFileObject::fgetcsv incorrectly returns a row on premature EOF
3+
--DESCRIPTION--
4+
This bug is about the different behavior with and without trailing newline both should behave the same,
5+
and neither should return FALSE.
6+
--FILE--
7+
<?php
8+
9+
$s = fopen("php://memory", "w+");
10+
fwrite($s, "\",bar");
11+
rewind($s);
12+
var_dump(fgetcsv($s));
13+
fclose($s);
14+
15+
$s = fopen("php://memory", "w+");
16+
fwrite($s, "\",bar\n");
17+
rewind($s);
18+
var_dump(fgetcsv($s));
19+
fclose($s);
20+
?>
21+
--EXPECT--
22+
array(1) {
23+
[0]=>
24+
string(4) ",bar"
25+
}
26+
array(1) {
27+
[0]=>
28+
string(5) ",bar
29+
"
30+
}

ext/standard/tests/file/fgetcsv.phpt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,9 @@ array(2) {
165165
string(4) "bbb
166166
"
167167
}
168-
bool(false)
168+
array(2) {
169+
[0]=>
170+
string(3) "aaa"
171+
[1]=>
172+
string(3) "bbb"
173+
}

0 commit comments

Comments
 (0)