Skip to content

Commit 9b5cb0e

Browse files
committed
Update fputcsv() to escape all characters equally.
At present, backslashes have special case handling within fputcsv(): when one is encountered within a field that's being escaped, escaping stops until the next instance of the enclosure character is hit. This can result in malformed CSV. Fixes bug #43225 (fputcsv incorrectly handles cells ending in \ followed by ").
1 parent e1410b5 commit 9b5cb0e

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ PHP NEWS
1212
- Core
1313
. Fixed bug #63943 (Bad warning text from strpos() on empty needle).
1414
(Laruence)
15+
. Fixed bug #43225 (fputcsv incorrectly handles cells ending in \ followed
16+
by "). (Adam)
1517

1618
- cURL extension:
1719
. Fixed bug (segfault due to libcurl connection caching). (Pierrick)

ext/standard/file.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,7 +1953,6 @@ PHP_FUNCTION(fputcsv)
19531953
{
19541954
char delimiter = ','; /* allow this to be set as parameter */
19551955
char enclosure = '"'; /* allow this to be set as parameter */
1956-
const char escape_char = '\\';
19571956
php_stream *stream;
19581957
int ret;
19591958
zval *fp = NULL, *fields = NULL, **field_tmp = NULL, field;
@@ -2008,24 +2007,19 @@ PHP_FUNCTION(fputcsv)
20082007
/* enclose a field that contains a delimiter, an enclosure character, or a newline */
20092008
if (FPUTCSV_FLD_CHK(delimiter) ||
20102009
FPUTCSV_FLD_CHK(enclosure) ||
2011-
FPUTCSV_FLD_CHK(escape_char) ||
20122010
FPUTCSV_FLD_CHK('\n') ||
20132011
FPUTCSV_FLD_CHK('\r') ||
20142012
FPUTCSV_FLD_CHK('\t') ||
2013+
FPUTCSV_FLD_CHK('\\') ||
20152014
FPUTCSV_FLD_CHK(' ')
20162015
) {
20172016
char *ch = Z_STRVAL(field);
20182017
char *end = ch + Z_STRLEN(field);
2019-
int escaped = 0;
20202018

20212019
smart_str_appendc(&csvline, enclosure);
20222020
while (ch < end) {
2023-
if (*ch == escape_char) {
2024-
escaped = 1;
2025-
} else if (!escaped && *ch == enclosure) {
2021+
if (*ch == enclosure) {
20262022
smart_str_appendc(&csvline, enclosure);
2027-
} else {
2028-
escaped = 0;
20292023
}
20302024
smart_str_appendc(&csvline, *ch);
20312025
ch++;

ext/standard/tests/file/fputcsv.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ echo '$list = ';var_export($res);echo ";\n";
4444

4545
$fp = fopen($file, "r");
4646
$res = array();
47-
while($l=fgetcsv($fp))
47+
while($l=fgetcsv($fp, 0, ',', '"', '"'))
4848
{
4949
$res[] = join(',',$l);
5050
}
@@ -75,10 +75,10 @@ $list = array (
7575
13 => 'aaa,"""bbb """',
7676
14 => '"aaa""aaa""","""bbb""bbb"',
7777
15 => '"aaa""aaa""""""",bbb',
78-
16 => 'aaa,"""\\"bbb",ccc',
79-
17 => '"aaa""\\"a""","""bbb"""',
80-
18 => '"""\\"""","""aaa"""',
81-
19 => '"""\\"""""",aaa',
78+
16 => 'aaa,"""\\""bbb",ccc',
79+
17 => '"aaa""\\""a""","""bbb"""',
80+
18 => '"""\\""""","""aaa"""',
81+
19 => '"""\\""""""",aaa',
8282
);
8383
$list = array (
8484
0 => 'aaa,bbb',
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
fputcsv(): bug #43225 (fputcsv incorrectly handles cells ending in \ followed by ")
3+
--FILE--
4+
<?php
5+
6+
$row = array(
7+
'a\\"',
8+
'bbb',
9+
);
10+
11+
$file = dirname(__FILE__) . 'fgetcsv_bug43225.csv';
12+
$fp = fopen($file, 'w');
13+
fputcsv($fp, $row);
14+
fclose($fp);
15+
readfile($file);
16+
unlink($file);
17+
18+
?>
19+
--EXPECT--
20+
"a\""",bbb

0 commit comments

Comments
 (0)