]> granicus.if.org Git - php/commitdiff
Tweak file_get_contents()'s return value a little
authorSara Golemon <pollita@php.net>
Fri, 22 Sep 2006 18:23:33 +0000 (18:23 +0000)
committerSara Golemon <pollita@php.net>
Fri, 22 Sep 2006 18:23:33 +0000 (18:23 +0000)
ext/standard/file.c

index 298735933ef08123ef100b6a0c8a65b342f83766..a8b7b124a21b87099d81a1a4cb7d7807c711d189 100644 (file)
@@ -512,7 +512,7 @@ PHP_FUNCTION(file_get_contents)
        char *filename;
        int filename_len;
        zend_uchar filename_type;
-       void *contents;
+       void *contents = NULL;
        long flags = 0;
        php_stream *stream;
        int len;
@@ -552,21 +552,32 @@ PHP_FUNCTION(file_get_contents)
        if (maxlen <= 0 || stream->readbuf_type == IS_STRING) {
                real_maxlen = maxlen;
        } else {
-               /* Allows worst case scenario of each input char being turned into two UChars */
-               real_maxlen = (maxlen * 2);
+               /* Allows worst case scenario of each input char being turned into two UChars
+                * UTODO: Have this take converter into account, since many never generate surrogate pairs */
+               real_maxlen = maxlen * 2;
        }
 
        /* uses mmap if possible */
        len = php_stream_copy_to_mem_ex(stream, stream->readbuf_type, &contents, real_maxlen, maxlen, 0);
 
-       if (stream->readbuf_type == IS_STRING && len > 0) {
-               RETVAL_STRINGL(contents, len, 0);
-       } else if (stream->readbuf_type == IS_UNICODE && len > 0) {
-               RETVAL_UNICODEL(contents, len, 0);
-       } else if (len == 0) {
-               RETVAL_EMPTY_STRING();
+       if (stream->readbuf_type == IS_STRING) {
+               if (len > 0) {
+                       RETVAL_STRINGL(contents, len, 0);
+               } else {
+                       if (contents) {
+                               efree(contents);
+                       }
+                       RETVAL_EMPTY_STRING();
+               }
        } else {
-               RETVAL_FALSE;
+               if (len > 0) {
+                       RETVAL_UNICODEL(contents, len, 0);
+               } else {
+                       if (contents) {
+                               efree(contents);
+                       }
+                       RETVAL_EMPTY_UNICODE();
+               }
        }
 
        php_stream_close(stream);