Skip to content

Fixed GH-18348: fputcsv incorrectly escapes when quote char is both enclosure and escape char #18372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions ext/standard/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -1788,16 +1788,11 @@ PHPAPI ssize_t php_fputcsv(php_stream *stream, zval *fields, char delimiter, cha
) {
char *ch = ZSTR_VAL(field_str);
char *end = ch + ZSTR_LEN(field_str);
int escaped = 0;

smart_str_appendc(&csvline, enclosure);
while (ch < end) {
if (escape_char != PHP_CSV_NO_ESCAPE && *ch == escape_char) {
escaped = 1;
} else if (!escaped && *ch == enclosure) {
if (*ch == enclosure) {
smart_str_appendc(&csvline, enclosure);
} else {
escaped = 0;
}
smart_str_appendc(&csvline, *ch);
ch++;
Expand Down
18 changes: 18 additions & 0 deletions ext/standard/tests/file/gh18348.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
GH-18348 (fputcsv incorrectly escapes when quote char is both enclosure and escape char)
--FILE--
<?php

$fp = fopen ('php://temp', 'r+');

fputcsv($fp, ['J"a"n'], ',', '"', 'a');
fputcsv($fp, ['He said "Hello"'], ',', '"', '"');
fputcsv($fp, ['Line1\\Line2'], ',', '"', '\\');

rewind($fp);
echo stream_get_contents($fp);
?>
--EXPECT--
"J""a""n"
"He said ""Hello"""
"Line1\Line2"
Loading