From cf7e15c3a0fb44f57298c1761a373ca08a6d4a9d Mon Sep 17 00:00:00 2001 From: Antony Dovgal Date: Fri, 11 Jul 2008 10:25:15 +0000 Subject: [PATCH] MFH: manage references of stream context properly --- ext/openssl/xp_ssl.c | 4 +- ext/standard/file.c | 4 -- ext/standard/http_fopen_wrapper.c | 3 ++ ext/standard/streamsfuncs.c | 21 +++++----- main/streams/streams.c | 9 ++++ main/streams/userspace.c | 69 +++++++++---------------------- main/streams/xp_socket.c | 4 +- 7 files changed, 49 insertions(+), 65 deletions(-) diff --git a/ext/openssl/xp_ssl.c b/ext/openssl/xp_ssl.c index d1d97940bf..3beefb6d23 100644 --- a/ext/openssl/xp_ssl.c +++ b/ext/openssl/xp_ssl.c @@ -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); + } } } diff --git a/ext/standard/file.c b/ext/standard/file.c index e8be7d92eb..af3a962fa3 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -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)); - } } /* }}} */ diff --git a/ext/standard/http_fopen_wrapper.c b/ext/standard/http_fopen_wrapper.c index 7382d38882..0140f2e94a 100644 --- a/ext/standard/http_fopen_wrapper.c +++ b/ext/standard/http_fopen_wrapper.c @@ -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; diff --git a/ext/standard/streamsfuncs.c b/ext/standard/streamsfuncs.c index 6649ec4cd6..279dd09ca0 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); } @@ -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); } /* }}} */ diff --git a/main/streams/streams.c b/main/streams/streams.c index 03aea23f60..14bbcd2657 100755 --- a/main/streams/streams.c +++ b/main/streams/streams.c @@ -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) { diff --git a/main/streams/userspace.c b/main/streams/userspace.c index b2b5c79b2f..452c2aab0f 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 3cdc91c451..2e4763e5b8 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); + } } } } -- 2.50.1