From a3cb589b715b16409b9eaf2ef1cd6881f1d8010c Mon Sep 17 00:00:00 2001 From: Ilia Alshanetsky Date: Mon, 24 Feb 2003 03:13:25 +0000 Subject: [PATCH] Fixed bug #22382 (fgetcsv did not handle \" correctly). --- ext/standard/file.c | 16 ++++++++++++++++ ext/standard/tests/file/bug22382.phpt | 27 +++++++++++++++++++++++++++ ext/standard/tests/file/test2.csv | 1 + 3 files changed, 44 insertions(+) create mode 100644 ext/standard/tests/file/bug22382.phpt create mode 100644 ext/standard/tests/file/test2.csv diff --git a/ext/standard/file.c b/ext/standard/file.c index fa7649b45c..810367f694 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -2558,6 +2558,21 @@ PHP_FUNCTION(fgetcsv) /* 2A. handle enclosure delimited field */ while (*bptr) { + /* we need to determine if the enclosure is 'real' or is it escaped */ + if (*(bptr - 1) == '\\') { + int escape_cnt = 0; + char *bptr_p = bptr - 2; + + while (bptr_p > buf && *bptr_p == '\\') { + escape_cnt++; + bptr_p--; + } + if (!(escape_cnt % 2)) { + goto normal_char; + continue; + } + } + if (*bptr == enclosure) { /* handle the enclosure */ if ( *(bptr+1) == enclosure) { @@ -2571,6 +2586,7 @@ PHP_FUNCTION(fgetcsv) break; /* .. from handling this field - resumes at 3. */ } } else { +normal_char: /* normal character */ *tptr++ = *bptr++; diff --git a/ext/standard/tests/file/bug22382.phpt b/ext/standard/tests/file/bug22382.phpt new file mode 100644 index 0000000000..5255eaf350 --- /dev/null +++ b/ext/standard/tests/file/bug22382.phpt @@ -0,0 +1,27 @@ +--TEST-- +Bug #22382: fgetcvs does not handle escaped quotes correctly +--POST-- +--GET-- +--FILE-- + +--EXPECT-- +array(6) { + [0]=> + string(3) "One" + [1]=> + string(7) "\"Two\"" + [2]=> + string(7) "Three\"" + [3]=> + string(4) "Four" + [4]=> + string(2) "\\" + [5]=> + string(28) "\\\\\\\\\\\\\\\\\\\\\\\"\\\\" +} \ No newline at end of file diff --git a/ext/standard/tests/file/test2.csv b/ext/standard/tests/file/test2.csv new file mode 100644 index 0000000000..d816464170 --- /dev/null +++ b/ext/standard/tests/file/test2.csv @@ -0,0 +1 @@ +"One","\"Two\"","Three\"","Four","\\","\\\\\\\\\\\\\\\\\\\\\\\"\\\\" -- 2.50.1