Skip to content

Commit a4191b0

Browse files
committed
Merge branch 'PHP-5.3' into PHP-5.4
* PHP-5.3: Update fputcsv() to escape all characters equally.
2 parents 6a06587 + 9b5cb0e commit a4191b0

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ PHP NEWS
88
. Fixed bug #63943 (Bad warning text from strpos() on empty needle).
99
(Laruence)
1010
. Fixed bug #63882 (zend_std_compare_objects crash on recursion). (Dmitry)
11+
. Fixed bug #43225 (fputcsv incorrectly handles cells ending in \ followed
12+
by "). (Adam)
1113
. Support BITMAPV5HEADER in getimagesize(). (AsamK, Lars)
1214

1315
- Litespeed:

ext/standard/file.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,20 +1871,16 @@ PHPAPI int php_fputcsv(php_stream *stream, zval *fields, char delimiter, char en
18711871
FPUTCSV_FLD_CHK('\n') ||
18721872
FPUTCSV_FLD_CHK('\r') ||
18731873
FPUTCSV_FLD_CHK('\t') ||
1874+
FPUTCSV_FLD_CHK('\\') ||
18741875
FPUTCSV_FLD_CHK(' ')
18751876
) {
18761877
char *ch = Z_STRVAL(field);
18771878
char *end = ch + Z_STRLEN(field);
1878-
int escaped = 0;
18791879

18801880
smart_str_appendc(&csvline, enclosure);
18811881
while (ch < end) {
1882-
if (*ch == escape_char) {
1883-
escaped = 1;
1884-
} else if (!escaped && *ch == enclosure) {
1882+
if (*ch == enclosure) {
18851883
smart_str_appendc(&csvline, enclosure);
1886-
} else {
1887-
escaped = 0;
18881884
}
18891885
smart_str_appendc(&csvline, *ch);
18901886
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)