]> granicus.if.org Git - php/commitdiff
MFH: manage references of stream context properly
authorAntony Dovgal <tony2001@php.net>
Fri, 11 Jul 2008 10:25:15 +0000 (10:25 +0000)
committerAntony Dovgal <tony2001@php.net>
Fri, 11 Jul 2008 10:25:15 +0000 (10:25 +0000)
ext/openssl/xp_ssl.c
ext/standard/file.c
ext/standard/http_fopen_wrapper.c
ext/standard/streamsfuncs.c
main/streams/streams.c
main/streams/userspace.c
main/streams/xp_socket.c

index d1d97940bfd0d441452c304459b9fa4ab5fdee12..3beefb6d23c68c22673029942af10e70c5fe7f7e 100644 (file)
@@ -556,7 +556,9 @@ static inline int php_openssl_tcp_sockop_accept(php_stream *stream, php_openssl_
                        
                        xparam->outputs.client = php_stream_alloc_rel(stream->ops, clisockdata, NULL, "r+");
                        if (xparam->outputs.client) {
-                               xparam->outputs.client->context = stream->context;
+                               if (stream->context) {
+                                       zend_list_addref(stream->context->rsrc_id);
+                               }
                        }
                }
 
index e8be7d92eb2961b1920dfbdf4c6920122f38a170..af3a962fa37de6a276cb79d7f8071b81622a0b3f 100644 (file)
@@ -884,10 +884,6 @@ PHP_NAMED_FUNCTION(php_if_fopen)
        }
 
        php_stream_to_zval(stream, return_value);
-
-       if (zcontext) {
-               zend_list_addref(Z_RESVAL_P(zcontext));
-       }
 }
 /* }}} */
 
index 7382d388827d989d0f2f8c70784985b540ff85bd..0140f2e94a4083c691de3388f1df88322b9f34ba 100644 (file)
@@ -611,6 +611,9 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper, char *path,
                if (location[0] != '\0')
                        php_stream_notify_info(context, PHP_STREAM_NOTIFY_REDIRECTED, location, 0);
 
+               if (context) { /* keep the context for the next try */
+                       zend_list_addref(context->rsrc_id);
+               }
                php_stream_close(stream);
                stream = NULL;
 
index 6649ec4cd67ffc33c8411db01bd45e0ddcca513e..279dd09ca0ca5445b8e9ac714af060b7bd365d6a 100644 (file)
@@ -100,6 +100,10 @@ PHP_FUNCTION(stream_socket_client)
        
        context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT);
 
+       if (context) {
+               zend_list_addref(context->rsrc_id);
+       }
+
        if (flags & PHP_STREAM_CLIENT_PERSISTENT) {
                spprintf(&hashkey, 0, "stream_socket_client__%s", host);
        }
@@ -155,10 +159,7 @@ PHP_FUNCTION(stream_socket_client)
        }
        
        php_stream_to_zval(stream, return_value);
-
-       if (zcontext) {
-               zend_list_addref(Z_RESVAL_P(zcontext));
-       }
+       
 }
 /* }}} */
 
@@ -182,6 +183,10 @@ PHP_FUNCTION(stream_socket_server)
        }
        
        context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT);
+       
+       if (context) {
+               zend_list_addref(context->rsrc_id);
+       }
 
        if (zerrno)     {
                zval_dtor(zerrno);
@@ -220,10 +225,6 @@ PHP_FUNCTION(stream_socket_server)
        }
        
        php_stream_to_zval(stream, return_value);
-
-       if (zcontext) {
-               zend_list_addref(Z_RESVAL_P(zcontext));
-       }
 }
 /* }}} */
 
@@ -1032,7 +1033,7 @@ PHP_FUNCTION(stream_context_get_default)
                FG(default_context) = php_stream_context_alloc();
        }
        context = FG(default_context);
-       
+
        if (params) {
                parse_context_options(context, params TSRMLS_CC);
        }
@@ -1062,7 +1063,7 @@ PHP_FUNCTION(stream_context_create)
                parse_context_params(context, params TSRMLS_CC);
        }
        
-       php_stream_context_to_zval(context, return_value);
+       RETURN_RESOURCE(context->rsrc_id);
 }
 /* }}} */
 
index 03aea23f60ef20370d6bc74a7ccb11d625970f91..14bbcd265781e0390af301109c22eeccb82d99bb 100755 (executable)
@@ -287,6 +287,7 @@ PHPAPI int _php_stream_free(php_stream *stream, int close_options TSRMLS_DC) /*
        int remove_rsrc = 1;
        int preserve_handle = close_options & PHP_STREAM_FREE_PRESERVE_HANDLE ? 1 : 0;
        int release_cast = 1;
+       php_stream_context *context = stream->context;
 
        if (stream->flags & PHP_STREAM_FLAG_NO_CLOSE) {
                preserve_handle = 1;
@@ -427,6 +428,10 @@ fprintf(stderr, "stream_free: %s:%p[%s] preserve_handle=%d release_cast=%d remov
 #endif
        }
 
+       if (context) {
+               zend_list_delete(context->rsrc_id);
+       }
+
        return ret;
 }
 /* }}} */
@@ -1801,6 +1806,10 @@ PHPAPI php_stream *_php_stream_open_wrapper_ex(char *path, char *mode, int optio
                                opened_path, context STREAMS_REL_CC TSRMLS_CC);
                }
 
+               if (context) {
+                       zend_list_addref(context->rsrc_id);
+               }
+
                /* if the caller asked for a persistent stream but the wrapper did not
                 * return one, force an error here */
                if (stream && (options & STREAM_OPEN_PERSISTENT) && !stream->is_persistent) {
index b2b5c79b2f792f49e58742a9cbfab40ad0b2512b..452c2aab0f6df6be6a8a32bc9fa05fc06483ab04 100644 (file)
@@ -215,7 +215,6 @@ static php_stream *user_wrapper_opener(php_stream_wrapper *wrapper, char *filena
        zval **args[4]; 
        int call_result;
        php_stream *stream = NULL;
-       zval *zcontext = NULL;
        zend_bool old_in_user_include;
 
        /* Try to catch bad usage without preventing flexibility */
@@ -281,12 +280,8 @@ static php_stream *user_wrapper_opener(php_stream_wrapper *wrapper, char *filena
        }
 
        if (context) {
-               MAKE_STD_ZVAL(zcontext);
-               php_stream_context_to_zval(context, zcontext);
-               add_property_zval(us->object, "context", zcontext);
-               /* The object property should be the only reference,
-                  'get rid' of our local reference. */
-               zval_ptr_dtor(&zcontext);
+               add_property_resource(us->object, "context", context->rsrc_id);
+               zend_list_addref(context->rsrc_id);
        } else {
                add_property_null(us->object, "context");
        }
@@ -362,7 +357,7 @@ static php_stream *user_wrapper_opendir(php_stream_wrapper *wrapper, char *filen
 {
        struct php_user_stream_wrapper *uwrap = (struct php_user_stream_wrapper*)wrapper->abstract;
        php_userstream_data_t *us;
-       zval *zfilename, *zoptions, *zretval = NULL, *zfuncname, *zcontext;
+       zval *zfilename, *zoptions, *zretval = NULL, *zfuncname;
        zval **args[2]; 
        int call_result;
        php_stream *stream = NULL;
@@ -384,12 +379,8 @@ static php_stream *user_wrapper_opendir(php_stream_wrapper *wrapper, char *filen
        Z_SET_ISREF_P(us->object);
 
        if (context) {
-               MAKE_STD_ZVAL(zcontext);
-               php_stream_context_to_zval(context, zcontext);
-               add_property_zval(us->object, "context", zcontext);
-               /* The object property should be the only reference,
-                  'get rid' of our local reference. */
-               zval_ptr_dtor(&zcontext);
+               add_property_resource(us->object, "context", context->rsrc_id);
+               zend_list_addref(context->rsrc_id);
        } else {
                add_property_null(us->object, "context");
        }
@@ -952,7 +943,7 @@ static int php_userstreamop_set_option(php_stream *stream, int option, int value
 static int user_wrapper_unlink(php_stream_wrapper *wrapper, char *url, int options, php_stream_context *context TSRMLS_DC)
 {
        struct php_user_stream_wrapper *uwrap = (struct php_user_stream_wrapper*)wrapper->abstract;
-       zval *zfilename, *zfuncname, *zretval, *zcontext;
+       zval *zfilename, *zfuncname, *zretval;
        zval **args[1];
        int call_result;
        zval *object;
@@ -965,12 +956,8 @@ static int user_wrapper_unlink(php_stream_wrapper *wrapper, char *url, int optio
        Z_SET_ISREF_P(object);
 
        if (context) {
-               MAKE_STD_ZVAL(zcontext);
-               php_stream_context_to_zval(context, zcontext);
-               add_property_zval(object, "context", zcontext);
-               /* The object property should be the only reference,
-                  'get rid' of our local reference. */
-               zval_ptr_dtor(&zcontext);
+               add_property_resource(object, "context", context->rsrc_id);
+               zend_list_addref(context->rsrc_id);
        } else {
                add_property_null(object, "context");
        }
@@ -1010,7 +997,7 @@ static int user_wrapper_unlink(php_stream_wrapper *wrapper, char *url, int optio
 static int user_wrapper_rename(php_stream_wrapper *wrapper, char *url_from, char *url_to, int options, php_stream_context *context TSRMLS_DC)
 {
        struct php_user_stream_wrapper *uwrap = (struct php_user_stream_wrapper*)wrapper->abstract;
-       zval *zold_name, *znew_name, *zfuncname, *zretval, *zcontext;
+       zval *zold_name, *znew_name, *zfuncname, *zretval;
        zval **args[2];
        int call_result;
        zval *object;
@@ -1023,12 +1010,8 @@ static int user_wrapper_rename(php_stream_wrapper *wrapper, char *url_from, char
        Z_SET_ISREF_P(object);
 
        if (context) {
-               MAKE_STD_ZVAL(zcontext);
-               php_stream_context_to_zval(context, zcontext);
-               add_property_zval(object, "context", zcontext);
-               /* The object property should be the only reference,
-                  'get rid' of our local reference. */
-               zval_ptr_dtor(&zcontext);
+               add_property_resource(object, "context", context->rsrc_id);
+               zend_list_addref(context->rsrc_id);
        } else {
                add_property_null(object, "context");
        }
@@ -1073,7 +1056,7 @@ static int user_wrapper_rename(php_stream_wrapper *wrapper, char *url_from, char
 static int user_wrapper_mkdir(php_stream_wrapper *wrapper, char *url, int mode, int options, php_stream_context *context TSRMLS_DC)
 {
        struct php_user_stream_wrapper *uwrap = (struct php_user_stream_wrapper*)wrapper->abstract;
-       zval *zfilename, *zmode, *zoptions, *zfuncname, *zretval, *zcontext;
+       zval *zfilename, *zmode, *zoptions, *zfuncname, *zretval;
        zval **args[3];
        int call_result;
        zval *object;
@@ -1086,12 +1069,8 @@ static int user_wrapper_mkdir(php_stream_wrapper *wrapper, char *url, int mode,
        Z_SET_ISREF_P(object);
 
        if (context) {
-               MAKE_STD_ZVAL(zcontext);
-               php_stream_context_to_zval(context, zcontext);
-               add_property_zval(object, "context", zcontext);
-               /* The object property should be the only reference,
-                  'get rid' of our local reference. */
-               zval_ptr_dtor(&zcontext);
+               add_property_resource(object, "context", context->rsrc_id);
+               zend_list_addref(context->rsrc_id);
        } else {
                add_property_null(object, "context");
        }
@@ -1142,7 +1121,7 @@ static int user_wrapper_mkdir(php_stream_wrapper *wrapper, char *url, int mode,
 static int user_wrapper_rmdir(php_stream_wrapper *wrapper, char *url, int options, php_stream_context *context TSRMLS_DC)
 {
        struct php_user_stream_wrapper *uwrap = (struct php_user_stream_wrapper*)wrapper->abstract;
-       zval *zfilename, *zoptions, *zfuncname, *zretval, *zcontext;
+       zval *zfilename, *zoptions, *zfuncname, *zretval;
        zval **args[3];
        int call_result;
        zval *object;
@@ -1155,12 +1134,8 @@ static int user_wrapper_rmdir(php_stream_wrapper *wrapper, char *url, int option
        Z_SET_ISREF_P(object);
 
        if (context) {
-               MAKE_STD_ZVAL(zcontext);
-               php_stream_context_to_zval(context, zcontext);
-               add_property_zval(object, "context", zcontext);
-               /* The object property should be the only reference,
-                  'get rid' of our local reference. */
-               zval_ptr_dtor(&zcontext);
+               add_property_resource(object, "context", context->rsrc_id);
+               zend_list_addref(context->rsrc_id);
        } else {
                add_property_null(object, "context");
        }
@@ -1206,7 +1181,7 @@ static int user_wrapper_rmdir(php_stream_wrapper *wrapper, char *url, int option
 static int user_wrapper_stat_url(php_stream_wrapper *wrapper, char *url, int flags, php_stream_statbuf *ssb, php_stream_context *context TSRMLS_DC)
 {
        struct php_user_stream_wrapper *uwrap = (struct php_user_stream_wrapper*)wrapper->abstract;
-       zval *zfilename, *zfuncname, *zretval, *zflags, *zcontext;
+       zval *zfilename, *zfuncname, *zretval, *zflags;
        zval **args[2]; 
        int call_result;
        zval *object;
@@ -1219,12 +1194,8 @@ static int user_wrapper_stat_url(php_stream_wrapper *wrapper, char *url, int fla
        Z_SET_ISREF_P(object);
 
        if (context) {
-               MAKE_STD_ZVAL(zcontext);
-               php_stream_context_to_zval(context, zcontext);
-               add_property_zval(object, "context", zcontext);
-               /* The object property should be the only reference,
-                  'get rid' of our local reference. */
-               zval_ptr_dtor(&zcontext);
+               add_property_resource(object, "context", context->rsrc_id);
+               zend_list_addref(context->rsrc_id);
        } else {
                add_property_null(object, "context");
        }
index 3cdc91c45148ae146e6848db0e45c1a75ce8a855..2e4763e5b8a860eb800ef7dc9871c70b6607cdae 100644 (file)
@@ -716,8 +716,10 @@ static inline int php_tcp_sockop_accept(php_stream *stream, php_netstream_data_t
 
                        xparam->outputs.client = php_stream_alloc_rel(stream->ops, clisockdata, NULL, "r+");
                        if (xparam->outputs.client) {
-                               /* TODO: addref ? */
                                xparam->outputs.client->context = stream->context;
+                               if (stream->context) {
+                                       zend_list_addref(stream->context->rsrc_id);
+                               }
                        }
                }
        }