iliaa           Fri Nov  7 16:40:48 2003 EDT

  Added files:                 (Branch: PHP_4_3)
    /php-src/ext/standard/tests/file    test3.csv bug26003.phpt 

  Modified files:              
    /php-src    NEWS 
    /php-src/ext/standard       file.c 
  Log:
  Fixed bug #26003 (Make fgetcsv() binary safe). (Ilia)
  
  
Index: php-src/NEWS
diff -u php-src/NEWS:1.1247.2.460 php-src/NEWS:1.1247.2.461
--- php-src/NEWS:1.1247.2.460   Thu Nov  6 15:34:57 2003
+++ php-src/NEWS        Fri Nov  7 16:40:45 2003
@@ -17,6 +17,7 @@
   after every mcrypt_generic_init() call). (Ilia)
 - Fixed bug #26025 (Segfault on glob() without GLOB_NOCHECK or GLOB_NOMAGIC
   under *BSD platforms). (Moriyoshi)
+- Fixed bug #26003 (Make fgetcsv() binary safe). (Ilia)
 - Fixed bug #25581 (getimagesize () return incorrect values on bitmap
   (os2) files). (Marcus)
 
Index: php-src/ext/standard/file.c
diff -u php-src/ext/standard/file.c:1.279.2.38 php-src/ext/standard/file.c:1.279.2.39
--- php-src/ext/standard/file.c:1.279.2.38      Thu Oct  9 21:38:01 2003
+++ php-src/ext/standard/file.c Fri Nov  7 16:40:46 2003
@@ -21,7 +21,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: file.c,v 1.279.2.38 2003/10/10 01:38:01 iliaa Exp $ */
+/* $Id: file.c,v 1.279.2.39 2003/11/07 21:40:46 iliaa Exp $ */
 
 /* Synced with php 3.0 revision 1.218 1999-06-16 [ssb] */
 
@@ -2161,7 +2161,7 @@
        /* first section exactly as php_fgetss */
 
        zval **fd, **bytes, **p_delim, **p_enclosure;
-       int len, temp_len;
+       int len, temp_len, buf_len;
        char *buf;
        php_stream *stream;
 
@@ -2225,7 +2225,7 @@
        /* needed because recv/read/gzread doesnt set null char at end */
        memset(buf, 0, len + 1);
 
-       if (php_stream_gets(stream, buf, len) == NULL) {
+       if (php_stream_get_line(stream, buf, len, &buf_len) == NULL) {
                efree(buf);
                RETURN_FALSE;
        }
@@ -2236,7 +2236,7 @@
 
        lineEnd = emalloc(len + 1);
        bptr = buf;
-       tptr = buf + strlen(buf) -1;
+       tptr = buf + buf_len -1;
        while ( isspace((int)*(unsigned char *)tptr) && (*tptr!=delimiter) && (tptr > 
bptr) ) tptr--;
        tptr++;
        strcpy(lineEnd, tptr);
@@ -2299,17 +2299,25 @@
                                        *tptr++ = *bptr++;
 
                                        if (*bptr == 0) {       /* embedded line end? 
*/
+                                               if ((bptr - buf) < buf_len) {
+                                                       while (*bptr == '\0') {
+                                                               *tptr++ = *bptr++;
+                                                       }
+                                                       continue;
+                                               }
+                                       
                                                *(tptr-1)=0;            /* remove 
space character added on reading line */
                                                strcat(temp, lineEnd);   /* add the 
embedded line end to the field */
 
                                                /* read a new line from input, as at 
start of routine */
                                                memset(buf, 0, len+1);
 
-                                               if (php_stream_gets(stream, buf, len) 
== NULL) {
+                                               if (php_stream_get_line(stream, buf, 
len, &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) { 
+                                                               *tptr = 0;
                                                                break;
                                                        }
                                                        
@@ -2323,7 +2331,7 @@
                                                temp_len += len;
                                                temp = erealloc(temp, temp_len+1);
                                                bptr = buf;
-                                               tptr = buf + strlen(buf) -1;
+                                               tptr = buf + buf_len -1;
                                                while (isspace((int)*(unsigned char 
*)tptr) && (*tptr!=delimiter) && (tptr > bptr)) 
                                                        tptr--;
                                                tptr++; 
@@ -2339,14 +2347,17 @@
                        }
                } else {
                        /* 2B. Handle non-enclosure field */
-                       while ((*bptr != delimiter) && *bptr) 
+                       while ((*bptr != delimiter) && ((bptr - buf) < buf_len)) 
                                *tptr++ = *bptr++;
                        *tptr=0;        /* terminate temporary string */
 
-                       if (strlen(temp)) {
+                       if ((tptr - temp)) {
                                tptr--;
                                while (isspace((int)*(unsigned char *)tptr) && 
(*tptr!=delimiter)) 
                                        *tptr-- = 0;    /* strip any trailing spaces */
+                               if (*tptr) {
+                                       tptr++;
+                               }
                        }
                        
                        if (*bptr == delimiter) 
@@ -2354,7 +2365,11 @@
                }
 
                /* 3. Now pass our field back to php */
-               add_next_index_string(return_value, temp, 1);
+               if (*tptr == '\0') {
+                       add_next_index_stringl(return_value, temp, (tptr - temp), 1);
+               } else {
+                       add_next_index_string(return_value, temp, 1);
+               }
                tptr = temp;
        } while (*bptr);
 

Index: php-src/ext/standard/tests/file/test3.csv
+++ php-src/ext/standard/tests/file/test3.csv
abc,de

Index: php-src/ext/standard/tests/file/bug26003.phpt
+++ php-src/ext/standard/tests/file/bug26003.phpt
--TEST--
Bug #26003 (fgetcsv() is not binary-safe)
--FILE--
<?php
$fp = fopen(dirname(__FILE__).'/test3.csv', 'r');
var_dump(fgetcsv($fp, 4096));
?>
--EXPECT--
array(2) {
  [0]=>
  string(3) "abc"
  [1]=>
  string(3) "de"
}

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to