From: Ilia Alshanetsky Date: Thu, 5 Dec 2002 20:01:19 +0000 (+0000) Subject: Fixed bug #12556, updated the test for this bug. X-Git-Tag: RELEASE_1_0b3~164 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7364e3b771c185d3f66bd433cd469a4c7f3aee21;p=php Fixed bug #12556, updated the test for this bug. --- diff --git a/ext/standard/file.c b/ext/standard/file.c index eafb54ea13..6c210d9206 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -2109,7 +2109,7 @@ PHP_FUNCTION(fgetcsv) /* first section exactly as php_fgetss */ zval **fd, **bytes, **p_delim, **p_enclosure; - int len; + int len, temp_len; char *buf; php_stream *stream; @@ -2194,7 +2194,8 @@ PHP_FUNCTION(fgetcsv) /* reserve workspace for building each individual field */ - temp = emalloc(len); /* unlikely but possible! */ + temp_len = len; + temp = emalloc(temp_len + 1); /* unlikely but possible! */ tptr = temp; /* Initialize return array */ @@ -2209,7 +2210,7 @@ PHP_FUNCTION(fgetcsv) /* 2. Read field, leaving bptr pointing at start of next field */ if (enclosure && *bptr == enclosure) { bptr++; /* move on to first character in field */ - + /* 2A. handle enclosure delimited field */ while (*bptr) { if (*bptr == enclosure) { @@ -2236,6 +2237,13 @@ PHP_FUNCTION(fgetcsv) memset(buf, 0, len+1); if (php_stream_gets(stream, buf, len) == NULL) { + /* we've got an unterminated enclosure, assign all the data + * from the start of the enclosure to end of data to the last element + */ + if (temp_len > len) { + break; + } + efree(lineEnd); efree(temp); efree(buf); @@ -2243,6 +2251,8 @@ PHP_FUNCTION(fgetcsv) RETURN_FALSE; } + temp_len += len; + temp = erealloc(temp, temp_len+1); bptr = buf; tptr = buf + strlen(buf) -1; while (isspace((int) *tptr) && (*tptr!=delimiter) && (tptr > bptr)) diff --git a/ext/standard/tests/file/bug12556.phpt b/ext/standard/tests/file/bug12556.phpt index b673d41b61..cac77e5f2b 100644 --- a/ext/standard/tests/file/bug12556.phpt +++ b/ext/standard/tests/file/bug12556.phpt @@ -4,18 +4,46 @@ Bug #12556: fgetcvs ignores lengths when quotes not closed --GET-- --FILE-- --EXPECT-- -Read 24 bytes -Read 24 bytes -Read 24 bytes -Read 24 bytes -Read 24 bytes -Read 24 bytes -Read 24 bytes -Read 24 bytes -Read 24 bytes +array(4) { + [0]=> + string(1) "6" + [1]=> + string(1) "7" + [2]=> + string(1) "8" + [3]=> + string(5) "line1" +} +array(4) { + [0]=> + string(1) "1" + [1]=> + string(1) "2" + [2]=> + string(1) "3" + [3]=> + string(186) "line2 +2,4,5,line3 +2,4,5,line3 +2,4,5,line3 +2,4,5,line3 +2,4,5,line3 +2,4,5,line3 +2,4,5,line3 +2,4,5,line3 +2,4,5,line3 +2,4,5,line3 +2,4,5,line3 +2,4,5,line3 +2,4,5,line3 +2,4,5,line3 +2,4,5,line3 +" +}