]> granicus.if.org Git - php/commitdiff
manage references of stream context properly
authorAntony Dovgal <tony2001@php.net>
Fri, 11 Jul 2008 10:24:29 +0000 (10:24 +0000)
committerAntony Dovgal <tony2001@php.net>
Fri, 11 Jul 2008 10:24:29 +0000 (10:24 +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 b0ed9b60f968ac36098531e1ad0dd10471ae9b91..ea7ffa63f18e0064318a866ead4eebd6057ee41e 100644 (file)
@@ -555,7 +555,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 90618f152ccae128e29382821a6d3abc87ab8975..26a99257059d23c50e9cac8f95532276119d99f7 100644 (file)
@@ -1000,10 +1000,6 @@ PHP_NAMED_FUNCTION(php_if_fopen)
        }
 
        php_stream_to_zval(stream, return_value);
-
-       if (zcontext) {
-               zend_list_addref(Z_RESVAL_P(zcontext));
-       }
 }
 /* }}} */
 
index 18fa9292bd89b0ac3c3f82407df233370068d50a..3c317327bbdac3e562b6905dc65ae82b9e3550e8 100644 (file)
@@ -659,6 +659,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 14bcebb2e775acb131edff64580f89888eb3a350..b2081dd950c5e22446799fea70484bac857d8212 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);
        }
@@ -156,10 +160,7 @@ PHP_FUNCTION(stream_socket_client)
        }
        
        php_stream_to_zval(stream, return_value);
-
-       if (zcontext) {
-               zend_list_addref(Z_RESVAL_P(zcontext));
-       }
+       
 }
 /* }}} */
 
@@ -183,6 +184,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);
@@ -222,10 +227,6 @@ PHP_FUNCTION(stream_socket_server)
        }
        
        php_stream_to_zval(stream, return_value);
-
-       if (zcontext) {
-               zend_list_addref(Z_RESVAL_P(zcontext));
-       }
 }
 /* }}} */
 
@@ -1147,7 +1148,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);
        }
@@ -1177,7 +1178,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 cbc3a25301a8360d1aa865ae7d092b0b655a001d..fe7f0ff915e983b78caf0b20d92e2f4585955dbc 100755 (executable)
@@ -288,6 +288,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;
@@ -428,6 +429,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;
 }
 /* }}} */
@@ -2414,6 +2419,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 88f3478ab456f1388de1d61bf6bf44623a9b6e56..6300404918811f7c6d7c7016dea5b8d49b0d967c 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 737f9d821a334a48754ab487cc438245b5e5a646..4d58aebcba7ffc62157ee01f1fb23bf14b6d4430 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);
+                               }
                        }
                }
        }