From: Antony Dovgal Date: Fri, 11 Jul 2008 10:24:29 +0000 (+0000) Subject: manage references of stream context properly X-Git-Tag: BEFORE_HEAD_NS_CHANGE~1312 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3519ed9e3179e3669b14a87b94f4c49cd1e6d7a2;p=php manage references of stream context properly --- diff --git a/ext/openssl/xp_ssl.c b/ext/openssl/xp_ssl.c index b0ed9b60f9..ea7ffa63f1 100644 --- a/ext/openssl/xp_ssl.c +++ b/ext/openssl/xp_ssl.c @@ -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); + } } } diff --git a/ext/standard/file.c b/ext/standard/file.c index 90618f152c..26a9925705 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -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)); - } } /* }}} */ diff --git a/ext/standard/http_fopen_wrapper.c b/ext/standard/http_fopen_wrapper.c index 18fa9292bd..3c317327bb 100644 --- a/ext/standard/http_fopen_wrapper.c +++ b/ext/standard/http_fopen_wrapper.c @@ -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; diff --git a/ext/standard/streamsfuncs.c b/ext/standard/streamsfuncs.c index 14bcebb2e7..b2081dd950 100644 --- a/ext/standard/streamsfuncs.c +++ b/ext/standard/streamsfuncs.c @@ -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); } /* }}} */ diff --git a/main/streams/streams.c b/main/streams/streams.c index cbc3a25301..fe7f0ff915 100755 --- a/main/streams/streams.c +++ b/main/streams/streams.c @@ -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) { diff --git a/main/streams/userspace.c b/main/streams/userspace.c index 88f3478ab4..6300404918 100644 --- a/main/streams/userspace.c +++ b/main/streams/userspace.c @@ -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"); } diff --git a/main/streams/xp_socket.c b/main/streams/xp_socket.c index 737f9d821a..4d58aebcba 100644 --- a/main/streams/xp_socket.c +++ b/main/streams/xp_socket.c @@ -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); + } } } }