]> granicus.if.org Git - php/commitdiff
Fixed bug #12556, updated the test for this bug.
authorIlia Alshanetsky <iliaa@php.net>
Thu, 5 Dec 2002 20:01:19 +0000 (20:01 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Thu, 5 Dec 2002 20:01:19 +0000 (20:01 +0000)
ext/standard/file.c
ext/standard/tests/file/bug12556.phpt

index eafb54ea13521b78e24a956fd83146edc094a289..6c210d9206fef81272b3fe8685180b391133121d 100644 (file)
@@ -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)) 
index b673d41b614b0b4e87ef11ac56bf9cad87c62deb..cac77e5f2b795ffd814ff06c8a741808319f21a6 100644 (file)
@@ -4,18 +4,46 @@ Bug #12556: fgetcvs ignores lengths when quotes not closed
 --GET--
 --FILE--
 <?php
-$fp=fopen(dirname(__FILE__)."/test.csv", "r");
-while($line=fgetcsv($fp, 24)){
-       print("Read 24 bytes\n");
+$fp = fopen(dirname(__FILE__)."/test.csv", "r");
+while($line = fgetcsv($fp, 24)) {
+       var_dump($line);
 }
+fclose($fp);
 ?>
 --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
+"
+}