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 9b5cb0e805.
This commit is contained in:
Adam Harvey 2013-01-15 17:33:54 +08:00
parent dc495bbe95
commit cab290d2ad
3 changed files with 11 additions and 27 deletions

View File

@ -1871,16 +1871,20 @@ PHPAPI int php_fputcsv(php_stream *stream, zval *fields, char delimiter, char en
FPUTCSV_FLD_CHK('\n') || FPUTCSV_FLD_CHK('\n') ||
FPUTCSV_FLD_CHK('\r') || FPUTCSV_FLD_CHK('\r') ||
FPUTCSV_FLD_CHK('\t') || FPUTCSV_FLD_CHK('\t') ||
FPUTCSV_FLD_CHK('\\') ||
FPUTCSV_FLD_CHK(' ') FPUTCSV_FLD_CHK(' ')
) { ) {
char *ch = Z_STRVAL(field); char *ch = Z_STRVAL(field);
char *end = ch + Z_STRLEN(field); char *end = ch + Z_STRLEN(field);
int escaped = 0;
smart_str_appendc(&csvline, enclosure); smart_str_appendc(&csvline, enclosure);
while (ch < end) { while (ch < end) {
if (*ch == enclosure) { if (*ch == escape_char) {
escaped = 1;
} else if (!escaped && *ch == enclosure) {
smart_str_appendc(&csvline, enclosure); smart_str_appendc(&csvline, enclosure);
} else {
escaped = 0;
} }
smart_str_appendc(&csvline, *ch); smart_str_appendc(&csvline, *ch);
ch++; ch++;

View File

@ -44,7 +44,7 @@ echo '$list = ';var_export($res);echo ";\n";
$fp = fopen($file, "r"); $fp = fopen($file, "r");
$res = array(); $res = array();
while($l=fgetcsv($fp, 0, ',', '"', '"')) while($l=fgetcsv($fp))
{ {
$res[] = join(',',$l); $res[] = join(',',$l);
} }
@ -75,10 +75,10 @@ $list = array (
13 => 'aaa,"""bbb """', 13 => 'aaa,"""bbb """',
14 => '"aaa""aaa""","""bbb""bbb"', 14 => '"aaa""aaa""","""bbb""bbb"',
15 => '"aaa""aaa""""""",bbb', 15 => '"aaa""aaa""""""",bbb',
16 => 'aaa,"""\\""bbb",ccc', 16 => 'aaa,"""\\"bbb",ccc',
17 => '"aaa""\\""a""","""bbb"""', 17 => '"aaa""\\"a""","""bbb"""',
18 => '"""\\""""","""aaa"""', 18 => '"""\\"""","""aaa"""',
19 => '"""\\""""""",aaa', 19 => '"""\\"""""",aaa',
); );
$list = array ( $list = array (
0 => 'aaa,bbb', 0 => 'aaa,bbb',

View File

@ -1,20 +0,0 @@
--TEST--
fputcsv(): bug #43225 (fputcsv incorrectly handles cells ending in \ followed by ")
--FILE--
<?php
$row = array(
'a\\"',
'bbb',
);
$file = dirname(__FILE__) . 'fgetcsv_bug43225.csv';
$fp = fopen($file, 'w');
fputcsv($fp, $row);
fclose($fp);
readfile($file);
unlink($file);
?>
--EXPECT--
"a\""",bbb