From 9213adc109d551153ab65c316db0705b30604abf Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Mon, 9 Nov 2026 00:31:26 +0000 Subject: [PATCH 1/2] tree: add new stream context macros and use them --- ext/fileinfo/fileinfo.c | 4 +- ext/hash/hash.c | 6 +-- ext/libxml/libxml.c | 9 +++- ext/libxml/php_libxml.h | 2 +- ext/pdo_pgsql/pgsql_driver.c | 4 +- ext/phar/func_interceptors.c | 8 +-- ext/soap/php_http.c | 2 +- ext/soap/php_sdl.c | 4 +- ext/soap/soap.c | 4 +- ext/spl/spl_directory.c | 4 +- ext/standard/dir.c | 4 +- ext/standard/file.c | 37 ++++++++----- ext/standard/streamsfuncs.c | 52 +++++++++---------- .../tests/general_functions/bug41518.phpt | 1 + ext/standard/url.c | 2 +- ext/zlib/zlib.c | 6 +-- main/php_streams.h | 1 - main/streams/php_stream_context.h | 42 ++++++++++----- main/streams/stream_errors.c | 2 +- 19 files changed, 109 insertions(+), 85 deletions(-) diff --git a/ext/fileinfo/fileinfo.c b/ext/fileinfo/fileinfo.c index 4ee4e3b6d9f9..3cc4165c3fba 100644 --- a/ext/fileinfo/fileinfo.c +++ b/ext/fileinfo/fileinfo.c @@ -300,7 +300,7 @@ PHP_FUNCTION(finfo_file) zend_argument_must_not_be_empty_error(hasThis() ? 1 : 2); RETURN_THROWS(); } - php_stream_context *context = php_stream_context_from_zval(zcontext, false); + php_stream_context *context = php_stream_context_from_zval_or_default(zcontext); if (!context) { RETURN_THROWS(); } @@ -424,7 +424,7 @@ PHP_FUNCTION(mime_content_type) const char *ret_val; if (path) { - php_stream_context *context = php_stream_context_get_default(false); + php_stream_context *context = php_stream_context_get_default(); ret_val = php_fileinfo_from_path(magic, path, context); } else { /* remember stream position for restoration */ diff --git a/ext/hash/hash.c b/ext/hash/hash.c index 419206212ebe..ebfb4d3eff69 100644 --- a/ext/hash/hash.c +++ b/ext/hash/hash.c @@ -356,7 +356,7 @@ static void php_hash_do_hash( zend_argument_value_error(2, "must not contain any null bytes"); RETURN_THROWS(); } - stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context)); + stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, php_stream_context_get_default()); if (!stream) { /* Stream will report errors opening file */ RETURN_FALSE; @@ -499,7 +499,7 @@ static void php_hash_do_hash_hmac( zend_argument_value_error(2, "must not contain any null bytes"); RETURN_THROWS(); } - stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context)); + stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, php_stream_context_get_default()); if (!stream) { /* Stream will report errors opening file */ RETURN_FALSE; @@ -743,7 +743,7 @@ PHP_FUNCTION(hash_update_file) hash = php_hashcontext_from_object(Z_OBJ_P(zhash)); PHP_HASHCONTEXT_VERIFY(hash); - context = php_stream_context_from_zval(zcontext, 0); + context = php_stream_context_from_zval_or_default(zcontext); stream = php_stream_open_wrapper_ex(ZSTR_VAL(filename), "rb", REPORT_ERRORS, NULL, context); if (!stream) { diff --git a/ext/libxml/libxml.c b/ext/libxml/libxml.c index d09ec299ff3c..3f46e1a68466 100644 --- a/ext/libxml/libxml.c +++ b/ext/libxml/libxml.c @@ -417,7 +417,11 @@ static PHP_GINIT_FUNCTION(libxml) PHP_LIBXML_API php_stream_context *php_libxml_get_stream_context(void) { - return php_stream_context_from_zval(Z_ISUNDEF(LIBXML(stream_context)) ? NULL : &LIBXML(stream_context), false); + php_stream_context *context = php_stream_context_get_default(); + if (!Z_ISUNDEF(LIBXML(stream_context))) { + context = php_stream_context_from_zval(&LIBXML(stream_context)); + } + return context; } /* Channel libxml file io layer through the PHP streams subsystem. @@ -1105,7 +1109,8 @@ PHP_FUNCTION(libxml_set_streams_context) Z_PARAM_RESOURCE(arg) ZEND_PARSE_PARAMETERS_END(); - if (php_stream_context_from_zval(arg, true) != NULL) { + php_stream_context *context = php_stream_context_from_zval(arg); + if (context != NULL) { if (!Z_ISUNDEF(LIBXML(stream_context))) { zval_ptr_dtor(&LIBXML(stream_context)); } diff --git a/ext/libxml/php_libxml.h b/ext/libxml/php_libxml.h index 5896f70803f8..576cd73c199d 100644 --- a/ext/libxml/php_libxml.h +++ b/ext/libxml/php_libxml.h @@ -40,7 +40,7 @@ extern zend_module_entry libxml_module_entry; #define LIBXML_NS_TAG_HOOK 1 ZEND_BEGIN_MODULE_GLOBALS(libxml) - zval stream_context; + zval stream_context; // TODO: Use php_stream_context* directly? smart_str error_buffer; zend_llist *error_list; zend_fcall_info_cache entity_loader_callback; diff --git a/ext/pdo_pgsql/pgsql_driver.c b/ext/pdo_pgsql/pgsql_driver.c index 62742cfb2c10..e29fa60c8aba 100644 --- a/ext/pdo_pgsql/pgsql_driver.c +++ b/ext/pdo_pgsql/pgsql_driver.c @@ -861,7 +861,7 @@ void pgsqlCopyFromFile_internal(INTERNAL_FUNCTION_PARAMETERS) PDO_CONSTRUCT_CHECK; PDO_DBH_CLEAR_ERR(); - stream = php_stream_open_wrapper_ex(filename, "rb", 0, NULL, FG(default_context)); + stream = php_stream_open_wrapper_ex(filename, "rb", 0, NULL, php_stream_context_get_default()); if (!stream) { pdo_pgsql_error_msg(dbh, PGRES_FATAL_ERROR, "Unable to open the file"); PDO_HANDLE_DBH_ERR(); @@ -966,7 +966,7 @@ void pgsqlCopyToFile_internal(INTERNAL_FUNCTION_PARAMETERS) H = (pdo_pgsql_db_handle *)dbh->driver_data; - stream = php_stream_open_wrapper_ex(filename, "wb", 0, NULL, FG(default_context)); + stream = php_stream_open_wrapper_ex(filename, "wb", 0, NULL, php_stream_context_get_default()); if (!stream) { pdo_pgsql_error_msg(dbh, PGRES_FATAL_ERROR, "Unable to open the file for writing"); PDO_HANDLE_DBH_ERR(); diff --git a/ext/phar/func_interceptors.c b/ext/phar/func_interceptors.c index e4cf1933b797..ab6948e604a1 100644 --- a/ext/phar/func_interceptors.c +++ b/ext/phar/func_interceptors.c @@ -64,7 +64,7 @@ PHP_FUNCTION(phar_opendir) /* {{{ */ zend_string_release_ex(entry, false); zend_string_release_ex(arch, false); if (zcontext) { - context = php_stream_context_from_zval(zcontext, 0); + context = php_stream_context_from_zval(zcontext); } stream = php_stream_opendir(name, REPORT_ERRORS, context); efree(name); @@ -185,7 +185,7 @@ PHP_FUNCTION(phar_file_get_contents) /* {{{ */ php_stream *stream; if (zcontext) { - context = php_stream_context_from_zval(zcontext, 0); + context = php_stream_context_from_zval(zcontext); } stream = php_stream_open_wrapper_ex(ZSTR_VAL(name), "rb", 0 | REPORT_ERRORS, NULL, context); @@ -245,7 +245,7 @@ PHP_FUNCTION(phar_readfile) /* {{{ */ } php_stream *stream; - php_stream_context *context = php_stream_context_from_zval(zcontext, 0); + php_stream_context *context = php_stream_context_from_zval_or_default(zcontext); stream = php_stream_open_wrapper_ex(ZSTR_VAL(name), "rb", 0 | REPORT_ERRORS, NULL, context); @@ -292,7 +292,7 @@ PHP_FUNCTION(phar_fopen) /* {{{ */ } php_stream *stream; - php_stream_context *context = php_stream_context_from_zval(zcontext, 0); + php_stream_context *context = php_stream_context_from_zval_or_default(zcontext); stream = php_stream_open_wrapper_ex(ZSTR_VAL(name), mode, 0 | REPORT_ERRORS, NULL, context); diff --git a/ext/soap/php_http.c b/ext/soap/php_http.c index 521b75f287f9..083aa21d1fb7 100644 --- a/ext/soap/php_http.c +++ b/ext/soap/php_http.c @@ -473,7 +473,7 @@ bool make_http_soap_request( tmp = Z_CLIENT_STREAM_CONTEXT_P(this_ptr); if (Z_TYPE_P(tmp) == IS_RESOURCE) { - context = php_stream_context_from_zval(tmp, 0); + context = php_stream_context_from_zval(tmp); } if (context && diff --git a/ext/soap/php_sdl.c b/ext/soap/php_sdl.c index 69ab56169c40..436513cc9174 100644 --- a/ext/soap/php_sdl.c +++ b/ext/soap/php_sdl.c @@ -327,7 +327,7 @@ void sdl_set_uri_credentials(sdlCtx *ctx, char *uri) return; } zval *context_ptr = &context; - ctx->context = php_stream_context_from_zval(context_ptr, 1); + ctx->context = php_stream_context_from_zval(context_ptr); if (!ctx->context) { return; @@ -3396,7 +3396,7 @@ sdlPtr get_sdl(zval *this_ptr, char *uri, zend_long cache_wsdl) if (instanceof_function(Z_OBJCE_P(this_ptr), soap_class_entry)) { tmp = Z_CLIENT_STREAM_CONTEXT_P(this_ptr); if (Z_TYPE_P(tmp) == IS_RESOURCE) { - context = php_stream_context_from_zval(tmp, 0); + context = php_stream_context_from_zval(tmp); /* Share a reference with new_context down below. * For new contexts, the reference is only in new_context so that doesn't need extra refcounting. */ GC_ADDREF(context->res); diff --git a/ext/soap/soap.c b/ext/soap/soap.c index 2c50aa346057..7dba05f3d79b 100644 --- a/ext/soap/soap.c +++ b/ext/soap/soap.c @@ -1428,7 +1428,7 @@ PHP_METHOD(SoapServer, handle) */ sapi_add_header("Content-Type: text/xml; charset=utf-8", sizeof("Content-Type: text/xml; charset=utf-8")-1, 1); - php_stream_context *context = php_stream_context_from_zval(NULL, false); + php_stream_context *context = php_stream_context_get_default(); php_stream *stream = php_stream_open_wrapper_ex(service->sdl->source, "rb", REPORT_ERRORS, NULL, context); if (stream) { php_stream_passthru(stream); @@ -2275,7 +2275,7 @@ PHP_METHOD(SoapClient, __construct) if ((tmp = zend_hash_str_find(ht, "stream_context", sizeof("stream_context")-1)) != NULL && Z_TYPE_P(tmp) == IS_RESOURCE) { - context = php_stream_context_from_zval(tmp, 1); + context = php_stream_context_from_zval(tmp); Z_ADDREF_P(tmp); } else { context = php_stream_context_alloc(); diff --git a/ext/spl/spl_directory.c b/ext/spl/spl_directory.c index b8d7f4576ac3..a56bc775e862 100644 --- a/ext/spl/spl_directory.c +++ b/ext/spl/spl_directory.c @@ -283,7 +283,7 @@ static void spl_filesystem_dir_open(spl_filesystem_object* intern, zend_string * bool skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS); intern->type = SPL_FS_DIR; - intern->u.dir.dirp = php_stream_opendir(ZSTR_VAL(path), REPORT_ERRORS, FG(default_context)); + intern->u.dir.dirp = php_stream_opendir(ZSTR_VAL(path), REPORT_ERRORS, php_stream_context_get_default()); if (intern->u.dir.dirp) { /* we prevent potential UAF with conflicting explicit fclose(), relying on the object destructor for this */ @@ -328,7 +328,7 @@ static zend_result spl_filesystem_file_open(spl_filesystem_object *intern, bool return FAILURE; } - intern->u.file.context = php_stream_context_from_zval(intern->u.file.zcontext, 0); + intern->u.file.context = php_stream_context_from_zval_or_default(intern->u.file.zcontext); intern->u.file.stream = php_stream_open_wrapper_ex(ZSTR_VAL(intern->file_name), ZSTR_VAL(intern->u.file.open_mode), (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, intern->u.file.context); if (!ZSTR_LEN(intern->file_name) || !intern->u.file.stream) { diff --git a/ext/standard/dir.c b/ext/standard/dir.c index f313d5f539d1..ce3790d093fd 100644 --- a/ext/standard/dir.c +++ b/ext/standard/dir.c @@ -117,7 +117,7 @@ static void _php_do_opendir(INTERNAL_FUNCTION_PARAMETERS, int createobject) Z_PARAM_RESOURCE_OR_NULL(zcontext) ZEND_PARSE_PARAMETERS_END(); - context = php_stream_context_from_zval(zcontext, 0); + context = php_stream_context_from_zval_or_default(zcontext); dirp = php_stream_opendir(dirname, REPORT_ERRORS, context); @@ -579,7 +579,7 @@ PHP_FUNCTION(scandir) } if (zcontext) { - context = php_stream_context_from_zval(zcontext, 0); + context = php_stream_context_from_zval_or_default(zcontext); } if (flags == PHP_SCANDIR_SORT_ASCENDING) { diff --git a/ext/standard/file.c b/ext/standard/file.c index 0aa6a5456fae..f6864dbf8601 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -401,7 +401,7 @@ PHP_FUNCTION(file_get_contents) } php_stream_error_operation_begin(); - context = php_stream_context_from_zval(zcontext, 0); + context = php_stream_context_from_zval_or_default(zcontext); stream = php_stream_open_wrapper_ex(filename, "rb", (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, context); @@ -461,7 +461,11 @@ PHP_FUNCTION(file_put_contents) } php_stream_error_operation_begin(); - context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT); + if (zcontext) { + context = php_stream_context_from_zval(zcontext); + } else if ((flags & PHP_FILE_NO_DEFAULT_CONTEXT) == 0) { + context = php_stream_context_get_default(); + } if (flags & PHP_FILE_APPEND) { mode[0] = 'a'; @@ -616,7 +620,12 @@ PHP_FUNCTION(file) skip_blank_lines = flags & PHP_FILE_SKIP_EMPTY_LINES; php_stream_error_operation_begin(); - context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT); + if (zcontext) { + context = php_stream_context_from_zval(zcontext); + if ((flags & PHP_FILE_NO_DEFAULT_CONTEXT) == 0) { + context = php_stream_context_get_default(); + } + } stream = php_stream_open_wrapper_ex(filename, "rb", (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, context); if (!stream) { @@ -751,7 +760,7 @@ PHP_FUNCTION(fopen) ZEND_PARSE_PARAMETERS_END(); php_stream_error_operation_begin(); - context = php_stream_context_from_zval(zcontext, 0); + context = php_stream_context_from_zval_or_default(zcontext); stream = php_stream_open_wrapper_ex(filename, mode, (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, context); php_stream_error_operation_end(context); @@ -1144,7 +1153,7 @@ PHP_FUNCTION(mkdir) zval *zcontext = NULL; zend_long mode = 0777; bool recursive = 0; - php_stream_context *context; + php_stream_context *context = NULL; ZEND_PARSE_PARAMETERS_START(1, 4) Z_PARAM_PATH(dir, dir_len) @@ -1154,7 +1163,7 @@ PHP_FUNCTION(mkdir) Z_PARAM_RESOURCE_OR_NULL(zcontext) ZEND_PARSE_PARAMETERS_END(); - context = php_stream_context_from_zval(zcontext, 0); + context = php_stream_context_from_zval_or_default(zcontext); php_stream_error_operation_begin(); RETVAL_BOOL(php_stream_mkdir(dir, (int)mode, (recursive ? PHP_STREAM_MKDIR_RECURSIVE : 0) | REPORT_ERRORS, context)); @@ -1168,7 +1177,7 @@ PHP_FUNCTION(rmdir) char *dir; size_t dir_len; zval *zcontext = NULL; - php_stream_context *context; + php_stream_context *context = NULL; ZEND_PARSE_PARAMETERS_START(1, 2) Z_PARAM_PATH(dir, dir_len) @@ -1176,7 +1185,7 @@ PHP_FUNCTION(rmdir) Z_PARAM_RESOURCE_OR_NULL(zcontext) ZEND_PARSE_PARAMETERS_END(); - context = php_stream_context_from_zval(zcontext, 0); + context = php_stream_context_from_zval_or_default(zcontext); php_stream_error_operation_begin(); RETVAL_BOOL(php_stream_rmdir(dir, REPORT_ERRORS, context)); @@ -1202,7 +1211,7 @@ PHP_FUNCTION(readfile) Z_PARAM_RESOURCE_OR_NULL(zcontext) ZEND_PARSE_PARAMETERS_END(); - context = php_stream_context_from_zval(zcontext, 0); + context = php_stream_context_from_zval_or_default(zcontext); php_stream_error_operation_begin(); stream = php_stream_open_wrapper_ex(filename, "rb", (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, context); @@ -1270,7 +1279,7 @@ PHP_FUNCTION(rename) size_t old_name_len, new_name_len; zval *zcontext = NULL; php_stream_wrapper *wrapper; - php_stream_context *context; + php_stream_context *context = NULL; ZEND_PARSE_PARAMETERS_START(2, 3) Z_PARAM_PATH(old_name, old_name_len) @@ -1280,7 +1289,7 @@ PHP_FUNCTION(rename) ZEND_PARSE_PARAMETERS_END(); php_stream_error_operation_begin(); - context = php_stream_context_from_zval(zcontext, 0); + context = php_stream_context_from_zval_or_default(zcontext); wrapper = php_stream_locate_url_wrapper(old_name, NULL, 0); @@ -1323,7 +1332,7 @@ PHP_FUNCTION(unlink) ZEND_PARSE_PARAMETERS_END(); php_stream_error_operation_begin(); - context = php_stream_context_from_zval(zcontext, 0); + context = php_stream_context_from_zval_or_default(zcontext); wrapper = php_stream_locate_url_wrapper(filename, NULL, 0); @@ -1503,7 +1512,7 @@ PHP_FUNCTION(copy) char *source, *target; size_t source_len, target_len; zval *zcontext = NULL; - php_stream_context *context; + php_stream_context *context = NULL; ZEND_PARSE_PARAMETERS_START(2, 3) Z_PARAM_PATH(source, source_len) @@ -1513,7 +1522,7 @@ PHP_FUNCTION(copy) ZEND_PARSE_PARAMETERS_END(); php_stream_error_operation_begin(); - context = php_stream_context_from_zval(zcontext, 0); + context = php_stream_context_from_zval_or_default(zcontext); if (php_stream_locate_url_wrapper(source, NULL, 0) == &php_plain_files_wrapper && php_check_open_basedir(source)) { php_stream_error_operation_end(context); diff --git a/ext/standard/streamsfuncs.c b/ext/standard/streamsfuncs.c index 834f25ef0e04..3f8fd95a691a 100644 --- a/ext/standard/streamsfuncs.c +++ b/ext/standard/streamsfuncs.c @@ -68,7 +68,7 @@ PHP_FUNCTION(stream_socket_pair) } php_stream_error_operation_begin(); - context = php_stream_context_from_zval(zcontext, 0); + context = php_stream_context_from_zval_or_default(zcontext); s1 = php_stream_sock_open_from_socket(pair[0], 0); if (s1 == NULL) { @@ -137,7 +137,11 @@ PHP_FUNCTION(stream_socket_client) } php_stream_error_operation_begin(); - context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT); + if (zcontext) { + context = php_stream_context_from_zval(zcontext); + } else if ((flags & PHP_FILE_NO_DEFAULT_CONTEXT) == 0) { + context = php_stream_context_get_default(); + } if (flags & PHP_STREAM_CLIENT_PERSISTENT) { zend_string *escaped = php_stream_escape_persistent_key(ZSTR_VAL(host), ZSTR_LEN(host)); @@ -232,7 +236,11 @@ PHP_FUNCTION(stream_socket_server) ZEND_PARSE_PARAMETERS_END(); php_stream_error_operation_begin(); - context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT); + if (zcontext) { + context = php_stream_context_from_zval(zcontext); + } else if ((flags & PHP_FILE_NO_DEFAULT_CONTEXT) == 0) { + context = php_stream_context_get_default(); + } if (zerrno) { ZEND_TRY_ASSIGN_REF_LONG(zerrno, 0); @@ -529,7 +537,7 @@ PHP_FUNCTION(stream_copy_to_stream) } php_stream_error_operation_begin(); - context = php_stream_context_from_zval(zcontext, 0); + context = php_stream_context_from_zval_or_default(zcontext); if (pos > 0 && php_stream_seek(src, pos, SEEK_SET) < 0) { php_stream_error_operation_end(context); @@ -791,7 +799,7 @@ PHP_FUNCTION(stream_select) ZEND_PARSE_PARAMETERS_END(); php_stream_error_operation_begin(); - php_stream_context *context = php_stream_context_from_zval(zcontext, 0); + php_stream_context *context = php_stream_context_from_zval_or_default(zcontext); /* The sets grow as streams are added, past FD_SETSIZE */ php_growable_fd_set rfds, wfds, efds; @@ -1020,13 +1028,12 @@ static php_stream_context *decode_context_param(zval *contextresource) PHP_FUNCTION(stream_context_get_options) { zval *zcontext; - php_stream_context *context; ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_RESOURCE(zcontext) ZEND_PARSE_PARAMETERS_END(); - context = decode_context_param(zcontext); + php_stream_context *context = decode_context_param(zcontext); if (!context) { zend_argument_type_error(1, "must be a valid stream/context"); RETURN_THROWS(); @@ -1040,7 +1047,6 @@ PHP_FUNCTION(stream_context_get_options) PHP_FUNCTION(stream_context_set_option) { zval *zcontext = NULL; - php_stream_context *context; zend_string *wrappername; HashTable *options; char *optionname = NULL; @@ -1065,7 +1071,8 @@ PHP_FUNCTION(stream_context_set_option) ZEND_PARSE_PARAMETERS_END(); /* figure out where the context is coming from exactly */ - if (!(context = decode_context_param(zcontext))) { + php_stream_context *context = decode_context_param(zcontext); + if (!context) { zend_argument_type_error(1, "must be a valid stream/context"); RETURN_THROWS(); } @@ -1104,7 +1111,6 @@ PHP_FUNCTION(stream_context_set_option) PHP_FUNCTION(stream_context_set_options) { zval *zcontext = NULL; - php_stream_context *context; HashTable *options; ZEND_PARSE_PARAMETERS_START(2, 2) @@ -1113,7 +1119,8 @@ PHP_FUNCTION(stream_context_set_options) ZEND_PARSE_PARAMETERS_END(); /* figure out where the context is coming from exactly */ - if (!(context = decode_context_param(zcontext))) { + php_stream_context *context = decode_context_param(zcontext); + if (!context) { zend_argument_type_error(1, "must be a valid stream/context"); RETURN_THROWS(); } @@ -1130,14 +1137,13 @@ PHP_FUNCTION(stream_context_set_params) { HashTable *params; zval *zcontext; - php_stream_context *context; ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_RESOURCE(zcontext) Z_PARAM_ARRAY_HT(params) ZEND_PARSE_PARAMETERS_END(); - context = decode_context_param(zcontext); + php_stream_context *context = decode_context_param(zcontext); if (!context) { zend_argument_type_error(1, "must be a valid stream/context"); RETURN_THROWS(); @@ -1155,13 +1161,12 @@ PHP_FUNCTION(stream_context_set_params) PHP_FUNCTION(stream_context_get_params) { zval *zcontext; - php_stream_context *context; ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_RESOURCE(zcontext) ZEND_PARSE_PARAMETERS_END(); - context = decode_context_param(zcontext); + php_stream_context *context = decode_context_param(zcontext); if (!context) { zend_argument_type_error(1, "must be a valid stream/context"); RETURN_THROWS(); @@ -1183,17 +1188,13 @@ PHP_FUNCTION(stream_context_get_params) PHP_FUNCTION(stream_context_get_default) { HashTable *params = NULL; - php_stream_context *context; ZEND_PARSE_PARAMETERS_START(0, 1) Z_PARAM_OPTIONAL Z_PARAM_ARRAY_HT_OR_NULL(params) ZEND_PARSE_PARAMETERS_END(); - if (FG(default_context) == NULL) { - FG(default_context) = php_stream_context_alloc(); - } - context = FG(default_context); + php_stream_context *context = php_stream_context_get_default(); if (params) { if (parse_context_options(context, params) == FAILURE) { @@ -1227,16 +1228,12 @@ static bool php_stream_context_options_has_error_settings(const HashTable *optio PHP_FUNCTION(stream_context_set_default) { HashTable *options; - php_stream_context *context; ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_ARRAY_HT(options) ZEND_PARSE_PARAMETERS_END(); - if (FG(default_context) == NULL) { - FG(default_context) = php_stream_context_alloc(); - } - context = FG(default_context); + php_stream_context *context = php_stream_context_get_default(); if (php_stream_context_options_has_error_settings(options)) { zend_value_error("Stream error handling options cannot be set on the default context"); @@ -1256,7 +1253,6 @@ PHP_FUNCTION(stream_context_create) { HashTable *options = NULL; HashTable *params = NULL; - php_stream_context *context; ZEND_PARSE_PARAMETERS_START(0, 2) Z_PARAM_OPTIONAL @@ -1264,7 +1260,7 @@ PHP_FUNCTION(stream_context_create) Z_PARAM_ARRAY_HT_OR_NULL(params) ZEND_PARSE_PARAMETERS_END(); - context = php_stream_context_alloc(); + php_stream_context *context = php_stream_context_alloc(); if (options) { if (parse_context_options(context, options) == FAILURE) { @@ -1705,7 +1701,7 @@ PHP_FUNCTION(stream_is_local) } php_stream_error_operation_begin(); - context = php_stream_context_from_zval(zcontext, 0); + context = php_stream_context_from_zval_or_default(zcontext); wrapper = php_stream_locate_url_wrapper(Z_STRVAL_P(zstream), NULL, 0); php_stream_error_operation_end(context); } diff --git a/ext/standard/tests/general_functions/bug41518.phpt b/ext/standard/tests/general_functions/bug41518.phpt index 3b5ad3a8336a..8b84d98a7982 100644 --- a/ext/standard/tests/general_functions/bug41518.phpt +++ b/ext/standard/tests/general_functions/bug41518.phpt @@ -5,6 +5,7 @@ Bug #41518 (file_exists() warns of open_basedir restriction on non-existent file $tmp_dir = __DIR__ . '/bug41518'; mkdir($tmp_dir); if (!is_dir($tmp_dir)) { + @unlink($tmp_dir); die("skip"); } @unlink($tmp_dir); diff --git a/ext/standard/url.c b/ext/standard/url.c index ac303027a722..4ccda45bd433 100644 --- a/ext/standard/url.c +++ b/ext/standard/url.c @@ -702,7 +702,7 @@ PHP_FUNCTION(get_headers) Z_PARAM_RESOURCE_OR_NULL(zcontext) ZEND_PARSE_PARAMETERS_END(); - context = php_stream_context_from_zval(zcontext, 0); + context = php_stream_context_from_zval_or_default(zcontext); if (!(stream = php_stream_open_wrapper_ex(url, "r", REPORT_ERRORS | STREAM_USE_URL | STREAM_ONLY_GET_HEADERS, NULL, context))) { RETURN_FALSE; diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index 75e2e5ae2718..a16b51e579f7 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -621,7 +621,7 @@ PHP_FUNCTION(gzfile) } /* using a stream here is a bit more efficient (resource wise) than php_gzopen_wrapper */ - stream = php_stream_gzopen(NULL, filename, "rb", flags, NULL, php_stream_context_from_zval(NULL, false) STREAMS_CC); + stream = php_stream_gzopen(NULL, filename, "rb", flags, NULL, php_stream_context_get_default() STREAMS_CC); if (!stream) { /* Error reporting is already done by stream code */ @@ -664,7 +664,7 @@ PHP_FUNCTION(gzopen) flags |= USE_PATH; } - stream = php_stream_gzopen(NULL, filename, mode, flags, NULL, php_stream_context_from_zval(NULL, false) STREAMS_CC); + stream = php_stream_gzopen(NULL, filename, mode, flags, NULL, php_stream_context_get_default() STREAMS_CC); if (!stream) { RETURN_FALSE; @@ -691,7 +691,7 @@ PHP_FUNCTION(readgzfile) flags |= USE_PATH; } - stream = php_stream_gzopen(NULL, filename, "rb", flags, NULL, php_stream_context_from_zval(NULL, false) STREAMS_CC); + stream = php_stream_gzopen(NULL, filename, "rb", flags, NULL, php_stream_context_get_default() STREAMS_CC); if (!stream) { RETURN_FALSE; diff --git a/main/php_streams.h b/main/php_streams.h index 54baead30fe3..ee6eb9f52d49 100644 --- a/main/php_streams.h +++ b/main/php_streams.h @@ -93,7 +93,6 @@ END_EXTERN_C() typedef struct _php_stream php_stream; typedef struct _php_stream_wrapper php_stream_wrapper; -typedef struct _php_stream_context php_stream_context; typedef struct _php_stream_filter php_stream_filter; #include "streams/php_stream_context.h" diff --git a/main/streams/php_stream_context.h b/main/streams/php_stream_context.h index 9373182f7c2e..08f24f003767 100644 --- a/main/streams/php_stream_context.h +++ b/main/streams/php_stream_context.h @@ -14,6 +14,8 @@ /* Stream context and status notification related definitions */ +typedef struct _php_stream_context php_stream_context; + /* callback for status notifications */ typedef void (*php_stream_notification_func)(php_stream_context *context, int notifycode, int severity, @@ -23,20 +25,6 @@ typedef void (*php_stream_notification_func)(php_stream_context *context, #define PHP_STREAM_NOTIFIER_PROGRESS 1 -/* TODO: Remove dependence on ext/standard/file.h for the default context global */ -#define php_stream_context_get_default(without_context) \ - (without_context) ? NULL : FG(default_context) ? FG(default_context) : \ - (FG(default_context) = php_stream_context_alloc()) - -/* Attempt to fetch context from the zval passed, - If no context was passed, use the default context - The default context has not yet been created, do it now. */ -#define php_stream_context_from_zval(zcontext, nocontext) ( \ - (zcontext) ? zend_fetch_resource_ex(zcontext, "Stream-Context", php_le_stream_context()) : \ - php_stream_context_get_default(nocontext)) - -#define php_stream_context_to_zval(context, zval) { ZVAL_RES(zval, (context)->res); GC_ADDREF((context)->res); } - typedef struct _php_stream_notifier php_stream_notifier; struct _php_stream_notifier { @@ -71,6 +59,32 @@ PHPAPI php_stream_notifier *php_stream_notification_alloc(void); PHPAPI void php_stream_notification_free(php_stream_notifier *notifier); END_EXTERN_C() +/* TODO: Remove dependence on ext/standard/file.h for the default context global */ +//#include "ext/standard/file.h" +#define php_stream_context_get_default() \ + FG(default_context) ? FG(default_context) : (FG(default_context) = php_stream_context_alloc()) + +/* Attempt to fetch context from the zval passed, + If no context was passed, use the default context + The default context has not yet been created, do it now. */ +#define php_stream_context_from_zval(zcontext) \ + zend_fetch_resource_ex(zcontext, "Stream-Context", php_le_stream_context()) + +//static inline php_stream_context *php_stream_context_from_zval_or_default(zval *zcontext) { +// php_stream_context *context = NULL; +// if (zcontext) { +// context = php_stream_context_from_zval(zcontext); +// } else { +// context = php_stream_context_get_default(); +// } +// return context; +//} + +#define php_stream_context_from_zval_or_default(zcontext) \ + zcontext ? php_stream_context_from_zval(zcontext) : php_stream_context_get_default() + +#define php_stream_context_to_zval(context, zval) { ZVAL_RES(zval, (context)->res); GC_ADDREF((context)->res); } + /* not all notification codes are implemented */ #define PHP_STREAM_NOTIFY_RESOLVE 1 #define PHP_STREAM_NOTIFY_CONNECT 2 diff --git a/main/streams/stream_errors.c b/main/streams/stream_errors.c index bcdc163ee1ef..8ce8d93b6121 100644 --- a/main/streams/stream_errors.c +++ b/main/streams/stream_errors.c @@ -473,7 +473,7 @@ PHPAPI void php_stream_error_operation_end(const php_stream_context *context) } if (context == NULL) { - context = FG(default_context); + context = php_stream_context_get_default(); } php_stream_error_mode error_mode = php_stream_get_error_mode(context); From d176290cf43e0be38beb71158bf3923a8cfc12f8 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Mon, 9 Nov 2026 14:56:49 +0000 Subject: [PATCH 2/2] streams: move default_context global to PHP core ones from std/file ones --- Zend/zend.c | 2 -- ext/standard/basic_functions.c | 2 -- ext/standard/file.c | 25 ------------------------- ext/standard/file.h | 1 - main/main.c | 2 ++ main/php_globals.h | 4 ++-- main/php_streams.h | 1 + main/streams/php_stream_context.h | 5 ++--- main/streams/streams.c | 26 +++++++++++++++++++++++++- 9 files changed, 32 insertions(+), 36 deletions(-) diff --git a/Zend/zend.c b/Zend/zend.c index 560a365318de..8035a8415ffe 100644 --- a/Zend/zend.c +++ b/Zend/zend.c @@ -40,8 +40,6 @@ #include "zend_enum.h" #include "zend_closures.h" #include "Optimizer/zend_optimizer.h" -#include "php.h" -#include "php_globals.h" // FIXME: Breaks the declaration of the function below #undef zenderror diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index b8fd17fdba28..ab839c00c6cf 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -418,8 +418,6 @@ PHP_RINIT_FUNCTION(basic) /* {{{ */ /* Initialize memory for last http headers */ ZVAL_UNDEF(&BG(last_http_headers)); - /* Setup default context */ - FG(default_context) = NULL; return SUCCESS; } /* }}} */ diff --git a/ext/standard/file.c b/ext/standard/file.c index f6864dbf8601..d68482a33704 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -96,30 +96,7 @@ php_file_globals file_globals; /* }}} */ -/* {{{ ZTS-stuff / Globals / Prototypes */ - -/* sharing globals is *evil* */ -static int le_stream_context = FAILURE; - -PHPAPI int php_le_stream_context(void) -{ - return le_stream_context; -} -/* }}} */ - /* {{{ Module-Stuff */ -static ZEND_RSRC_DTOR_FUNC(file_context_dtor) -{ - php_stream_context *context = (php_stream_context*)res->ptr; - if (context == FG(default_context)) { - FG(default_context) = NULL; - } - if (Z_TYPE(context->options) != IS_UNDEF) { - zval_ptr_dtor(&context->options); - ZVAL_UNDEF(&context->options); - } - php_stream_context_free(context); -} static void file_globals_ctor(php_file_globals *file_globals_p) { @@ -143,8 +120,6 @@ PHP_INI_END() PHP_MINIT_FUNCTION(file) { - le_stream_context = zend_register_list_destructors_ex(file_context_dtor, NULL, "stream-context", module_number); - #ifdef ZTS ts_allocate_id(&file_globals_id, sizeof(php_file_globals), (ts_allocate_ctor) file_globals_ctor, (ts_allocate_dtor) file_globals_dtor); #else diff --git a/ext/standard/file.h b/ext/standard/file.h index 68f418d9ef0e..224398e4ff13 100644 --- a/ext/standard/file.h +++ b/ext/standard/file.h @@ -93,7 +93,6 @@ typedef struct { size_t def_chunk_size; zend_string *user_agent; /* for the http wrapper */ zend_string *from_address; /* for the ftp and http wrappers */ - php_stream_context *default_context; bool pclose_wait; #ifdef HAVE_GETHOSTBYNAME_R struct hostent tmp_host_info; diff --git a/main/main.c b/main/main.c index 0267c6839137..f9b9695fa3d5 100644 --- a/main/main.c +++ b/main/main.c @@ -1901,6 +1901,7 @@ zend_result php_request_startup(void) #if PHP_SIGCHILD signal(SIGCHLD, sigchld_handler); #endif + PG(default_context) = NULL; /* Default to global wrappers only */ PG(stream_wrappers) = NULL; @@ -2354,6 +2355,7 @@ zend_result php_module_startup(sapi_module_struct *sf, zend_module_entry *additi * (this uses configuration parameters from php.ini) */ php_init_stream_wrappers(module_number); + php_stream_context_init(module_number); zuv.html_errors = 1; php_startup_auto_globals(); diff --git a/main/php_globals.h b/main/php_globals.h index 876c57df9061..2ab61a4f2349 100644 --- a/main/php_globals.h +++ b/main/php_globals.h @@ -15,9 +15,8 @@ #ifndef PHP_GLOBALS_H #define PHP_GLOBALS_H -#include "zend_globals.h" - #include +#include "php_streams.h" typedef struct _php_core_globals php_core_globals; @@ -176,6 +175,7 @@ struct _php_core_globals { zend_long syslog_filter; zend_long error_log_mode; zend_long default_socket_timeout; + php_stream_context *default_context; const char *user_stream_current_filename; /* for simple recursion protection */ HashTable *stream_wrappers; /* per-request copy of url_stream_wrappers_hash */ HashTable *stream_filters; /* per-request copy of stream_filters_hash */ diff --git a/main/php_streams.h b/main/php_streams.h index ee6eb9f52d49..5233127b9c65 100644 --- a/main/php_streams.h +++ b/main/php_streams.h @@ -614,6 +614,7 @@ END_EXTERN_C() /* this flag is only used by include/require functions */ #define STREAM_OPEN_FOR_ZEND_STREAM 0x00010000 +void php_stream_context_init(int module_number); void php_init_stream_wrappers(int module_number); void php_shutdown_stream_wrappers(int module_number); void php_shutdown_stream_hashes(void); diff --git a/main/streams/php_stream_context.h b/main/streams/php_stream_context.h index 08f24f003767..6cd0b38c1baf 100644 --- a/main/streams/php_stream_context.h +++ b/main/streams/php_stream_context.h @@ -59,10 +59,9 @@ PHPAPI php_stream_notifier *php_stream_notification_alloc(void); PHPAPI void php_stream_notification_free(php_stream_notifier *notifier); END_EXTERN_C() -/* TODO: Remove dependence on ext/standard/file.h for the default context global */ -//#include "ext/standard/file.h" +//#include "php_globals.h" #define php_stream_context_get_default() \ - FG(default_context) ? FG(default_context) : (FG(default_context) = php_stream_context_alloc()) + PG(default_context) ? PG(default_context) : (PG(default_context) = php_stream_context_alloc()) /* Attempt to fetch context from the zval passed, If no context was passed, use the default context diff --git a/main/streams/streams.c b/main/streams/streams.c index 3e948add11fd..581796a4e921 100644 --- a/main/streams/streams.c +++ b/main/streams/streams.c @@ -39,6 +39,7 @@ static HashTable url_stream_wrappers_hash; static int le_stream = -1; /* true global */ static int le_pstream = -1; /* true global */ static int le_stream_filter = -1; /* true global */ +static int le_stream_context = -1; /* true global */ PHPAPI int php_file_le_stream(void) { @@ -55,6 +56,11 @@ PHPAPI int php_file_le_stream_filter(void) return le_stream_filter; } +PHPAPI int php_le_stream_context(void) +{ + return le_stream_context; +} + PHPAPI HashTable *php_stream_get_url_stream_wrappers_hash(void) { return (PG(stream_wrappers) ? PG(stream_wrappers) : &url_stream_wrappers_hash); @@ -2251,7 +2257,7 @@ PHPAPI php_stream *_php_stream_open_wrapper_ex(const char *path, const char *mod * stream_context_set_option() on the stream mutate the global default * context, leaking options into every other stream. Stream errors fall * back to the default context on their own when the stream has none. */ - if (stream->ctx == NULL && context != NULL && context != FG(default_context) && !persistent) { + if (stream->ctx == NULL && context != NULL && context != PG(default_context) && !persistent) { php_stream_context_set(stream, context); } @@ -2367,6 +2373,24 @@ PHPAPI void php_stream_notification_free(php_stream_notifier *notifier) efree(notifier); } + +static ZEND_RSRC_DTOR_FUNC(file_context_dtor) +{ + php_stream_context *context = (php_stream_context*)res->ptr; + if (context == PG(default_context)) { + PG(default_context) = NULL; + } + if (Z_TYPE(context->options) != IS_UNDEF) { + zval_ptr_dtor(&context->options); + ZVAL_UNDEF(&context->options); + } + php_stream_context_free(context); +} + +void php_stream_context_init(int module_number) { + le_stream_context = zend_register_list_destructors_ex(file_context_dtor, NULL, "stream-context", module_number); +} + PHPAPI zval *php_stream_context_get_option(const php_stream_context *context, const char *wrappername, const char *optionname) {