nikic                                    Sun, 19 Feb 2012 14:39:29 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=323362

Log:
Fix bug #61139 gzopen leaks when specifying invalid mode

Needs to be merged to 5.4.

Bug: https://bugs.php.net/61139 (Open) gzopen leaks when specifying invalid mode
      
Changed paths:
    A   php/php-src/branches/PHP_5_3/ext/zlib/tests/bug61139.phpt
    U   php/php-src/branches/PHP_5_3/ext/zlib/zlib_fopen_wrapper.c
    A   php/php-src/trunk/ext/zlib/tests/bug61139.phpt
    U   php/php-src/trunk/ext/zlib/zlib_fopen_wrapper.c

Added: php/php-src/branches/PHP_5_3/ext/zlib/tests/bug61139.phpt
===================================================================
--- php/php-src/branches/PHP_5_3/ext/zlib/tests/bug61139.phpt                   
        (rev 0)
+++ php/php-src/branches/PHP_5_3/ext/zlib/tests/bug61139.phpt   2012-02-19 
14:39:29 UTC (rev 323362)
@@ -0,0 +1,14 @@
+--TEST--
+Bug #61139 (gzopen leaks when specifying invalid mode)
+--SKIPIF--
+<?php
+if (!extension_loaded('zlib')) {
+       die('skip - zlib extension not loaded');
+}
+?>
+--FILE--
+<?php
+
+gzopen('someFile', 'c');
+--EXPECTF--
+Warning: gzopen(): gzopen failed in %s on line %d

Modified: php/php-src/branches/PHP_5_3/ext/zlib/zlib_fopen_wrapper.c
===================================================================
--- php/php-src/branches/PHP_5_3/ext/zlib/zlib_fopen_wrapper.c  2012-02-19 
14:35:47 UTC (rev 323361)
+++ php/php-src/branches/PHP_5_3/ext/zlib/zlib_fopen_wrapper.c  2012-02-19 
14:39:29 UTC (rev 323362)
@@ -109,7 +109,7 @@
 php_stream *php_stream_gzopen(php_stream_wrapper *wrapper, char *path, char 
*mode, int options,
                                                          char **opened_path, 
php_stream_context *context STREAMS_DC TSRMLS_DC)
 {
-       struct php_gz_stream_data_t *self = {0};
+       struct php_gz_stream_data_t *self;
        php_stream *stream = NULL, *innerstream = NULL;

        /* sanity check the stream: it can be either read-only or write-only */
@@ -120,8 +120,6 @@
                return NULL;
        }

-       self = emalloc(sizeof(*self));
-
        if (strncasecmp("compress.zlib://", path, 16) == 0) {
                path += 16;
        } else if (strncasecmp("zlib:", path, 5) == 0) {
@@ -134,32 +132,29 @@
                int fd;

                if (SUCCESS == php_stream_cast(innerstream, PHP_STREAM_AS_FD, 
(void **) &fd, REPORT_ERRORS)) {
-                       self->gz_file = gzdopen(dup(fd), mode);
+                       self = emalloc(sizeof(*self));
                        self->stream = innerstream;
-                       if (self->gz_file)      {
+                       self->gz_file = gzdopen(dup(fd), mode);
+
+                       if (self->gz_file) {
                                stream = 
php_stream_alloc_rel(&php_stream_gzio_ops, self, 0, mode);
                                if (stream) {
                                        stream->flags |= 
PHP_STREAM_FLAG_NO_BUFFER;
                                        return stream;
                                }
+
                                gzclose(self->gz_file);
                        }
+
+                       efree(self);
                        if (options & REPORT_ERRORS) {
                                php_error_docref(NULL TSRMLS_CC, E_WARNING, 
"gzopen failed");
                        }
-               } else if (innerstream) {
-                       php_stream_close(innerstream);
                }
-       }

-       if (stream) {
-               php_stream_close(stream);
+               php_stream_close(innerstream);
        }
-
-       if (self) {
-               efree(self);
-       }
-
+
        return NULL;
 }


Added: php/php-src/trunk/ext/zlib/tests/bug61139.phpt
===================================================================
--- php/php-src/trunk/ext/zlib/tests/bug61139.phpt                              
(rev 0)
+++ php/php-src/trunk/ext/zlib/tests/bug61139.phpt      2012-02-19 14:39:29 UTC 
(rev 323362)
@@ -0,0 +1,14 @@
+--TEST--
+Bug #61139 (gzopen leaks when specifying invalid mode)
+--SKIPIF--
+<?php
+if (!extension_loaded('zlib')) {
+       die('skip - zlib extension not loaded');
+}
+?>
+--FILE--
+<?php
+
+gzopen('someFile', 'c');
+--EXPECTF--
+Warning: gzopen(): gzopen failed in %s on line %d

Modified: php/php-src/trunk/ext/zlib/zlib_fopen_wrapper.c
===================================================================
--- php/php-src/trunk/ext/zlib/zlib_fopen_wrapper.c     2012-02-19 14:35:47 UTC 
(rev 323361)
+++ php/php-src/trunk/ext/zlib/zlib_fopen_wrapper.c     2012-02-19 14:39:29 UTC 
(rev 323362)
@@ -109,7 +109,7 @@
 php_stream *php_stream_gzopen(php_stream_wrapper *wrapper, char *path, char 
*mode, int options,
                                                          char **opened_path, 
php_stream_context *context STREAMS_DC TSRMLS_DC)
 {
-       struct php_gz_stream_data_t *self = {0};
+       struct php_gz_stream_data_t *self;
        php_stream *stream = NULL, *innerstream = NULL;

        /* sanity check the stream: it can be either read-only or write-only */
@@ -120,8 +120,6 @@
                return NULL;
        }

-       self = emalloc(sizeof(*self));
-
        if (strncasecmp("compress.zlib://", path, 16) == 0) {
                path += 16;
        } else if (strncasecmp("zlib:", path, 5) == 0) {
@@ -134,32 +132,29 @@
                int fd;

                if (SUCCESS == php_stream_cast(innerstream, PHP_STREAM_AS_FD, 
(void **) &fd, REPORT_ERRORS)) {
-                       self->gz_file = gzdopen(dup(fd), mode);
+                       self = emalloc(sizeof(*self));
                        self->stream = innerstream;
-                       if (self->gz_file)      {
+                       self->gz_file = gzdopen(dup(fd), mode);
+
+                       if (self->gz_file) {
                                stream = 
php_stream_alloc_rel(&php_stream_gzio_ops, self, 0, mode);
                                if (stream) {
                                        stream->flags |= 
PHP_STREAM_FLAG_NO_BUFFER;
                                        return stream;
                                }
+
                                gzclose(self->gz_file);
                        }
+
+                       efree(self);
                        if (options & REPORT_ERRORS) {
                                php_error_docref(NULL TSRMLS_CC, E_WARNING, 
"gzopen failed");
                        }
-               } else if (innerstream) {
-                       php_stream_close(innerstream);
                }
-       }

-       if (stream) {
-               php_stream_close(stream);
+               php_stream_close(innerstream);
        }
-
-       if (self) {
-               efree(self);
-       }
-
+
        return NULL;
 }


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

Reply via email to