Skip to content

Commit c077074

Browse files
committed
Revert "Update fputcsv() to escape all characters equally."
On second thoughts, while the behaviour _is_ broken, this isn't the right fix. This reverts commit 9b5cb0e.
1 parent 9b5cb0e commit c077074

File tree

4 files changed

+13
-29
lines changed

4 files changed

+13
-29
lines changed

NEWS

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ 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)
1715

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

ext/standard/file.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -1953,6 +1953,7 @@ 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 = '\\';
19561957
php_stream *stream;
19571958
int ret;
19581959
zval *fp = NULL, *fields = NULL, **field_tmp = NULL, field;
@@ -2007,19 +2008,24 @@ PHP_FUNCTION(fputcsv)
20072008
/* enclose a field that contains a delimiter, an enclosure character, or a newline */
20082009
if (FPUTCSV_FLD_CHK(delimiter) ||
20092010
FPUTCSV_FLD_CHK(enclosure) ||
2011+
FPUTCSV_FLD_CHK(escape_char) ||
20102012
FPUTCSV_FLD_CHK('\n') ||
20112013
FPUTCSV_FLD_CHK('\r') ||
20122014
FPUTCSV_FLD_CHK('\t') ||
2013-
FPUTCSV_FLD_CHK('\\') ||
20142015
FPUTCSV_FLD_CHK(' ')
20152016
) {
20162017
char *ch = Z_STRVAL(field);
20172018
char *end = ch + Z_STRLEN(field);
2019+
int escaped = 0;
20182020

20192021
smart_str_appendc(&csvline, enclosure);
20202022
while (ch < end) {
2021-
if (*ch == enclosure) {
2023+
if (*ch == escape_char) {
2024+
escaped = 1;
2025+
} else if (!escaped && *ch == enclosure) {
20222026
smart_str_appendc(&csvline, enclosure);
2027+
} else {
2028+
escaped = 0;
20232029
}
20242030
smart_str_appendc(&csvline, *ch);
20252031
ch++;

ext/standard/tests/file/fputcsv.phpt

+5-5
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, 0, ',', '"', '"'))
47+
while($l=fgetcsv($fp))
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',

ext/standard/tests/file/fputcsv_bug43225.phpt

-20
This file was deleted.

0 commit comments

Comments
 (0)