]> granicus.if.org Git - php/commitdiff
MFH: BugFix#28868 Wrapper hash not thread-safe
authorSara Golemon <pollita@php.net>
Mon, 21 Jun 2004 19:33:48 +0000 (19:33 +0000)
committerSara Golemon <pollita@php.net>
Mon, 21 Jun 2004 19:33:48 +0000 (19:33 +0000)
ext/standard/basic_functions.c
ext/standard/file.h
main/php_streams.h
main/streams.c
main/user_streams.c

index 731ddd4942825770c13df66ae99cde0092469822..2939dd6173308edf571a228a788c68b148950ea0 100644 (file)
@@ -1188,6 +1188,9 @@ PHP_RINIT_FUNCTION(basic)
        /* Reset magic_quotes_runtime */
        PG(magic_quotes_runtime) = INI_BOOL("magic_quotes_runtime");
 
+       /* Default to global wrappers only */
+       FG(stream_wrappers) = NULL;
+
        return SUCCESS;
 }
 
@@ -1211,6 +1214,12 @@ PHP_RSHUTDOWN_FUNCTION(basic)
        }
        STR_FREE(BG(locale_string));
 
+       if (FG(stream_wrappers)) {
+               zend_hash_destroy(FG(stream_wrappers));
+               efree(FG(stream_wrappers));
+               FG(stream_wrappers) = NULL;
+       }
+
        PHP_RSHUTDOWN(fsock) (SHUTDOWN_FUNC_ARGS_PASSTHRU);
        PHP_RSHUTDOWN(filestat) (SHUTDOWN_FUNC_ARGS_PASSTHRU);
        PHP_RSHUTDOWN(syslog) (SHUTDOWN_FUNC_ARGS_PASSTHRU);
index 5bee48c7ea0f89df07162b4f1568b551c0053980..7109789abb660f5211ff0ea43b125093155b3ae5 100644 (file)
@@ -118,6 +118,7 @@ typedef struct {
        long default_socket_timeout;
        char *user_agent;
        char *user_stream_current_filename; /* for simple recursion protection */
+       HashTable *stream_wrappers;                     /* per-request copy of url_stream_wrappers_hash */
 } php_file_globals;
 
 #ifdef ZTS
index 636dbf07bf22b5035a6de3fc815e587d0f8e234c..52c6f8d8862844940008bdce50641e35a2ddbbe9 100755 (executable)
@@ -530,6 +530,7 @@ PHP_RSHUTDOWN_FUNCTION(streams);
 
 PHPAPI int php_register_url_stream_wrapper(char *protocol, php_stream_wrapper *wrapper TSRMLS_DC);
 PHPAPI int php_unregister_url_stream_wrapper(char *protocol TSRMLS_DC);
+PHPAPI int php_register_url_stream_wrapper_volatile(char *protocol, php_stream_wrapper *wrapper TSRMLS_DC);
 PHPAPI php_stream *_php_stream_open_wrapper_ex(char *path, char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC);
 PHPAPI php_stream_wrapper *php_stream_locate_url_wrapper(const char *path, char **path_for_open, int options TSRMLS_DC);
 PHPAPI char *php_stream_locate_eol(php_stream *stream, char *buf, size_t buf_len TSRMLS_DC);
@@ -627,7 +628,8 @@ PHPAPI php_stream_context *php_stream_context_set(php_stream *stream, php_stream
        
 
 /* Give other modules access to the url_stream_wrappers_hash */
-PHPAPI HashTable *php_stream_get_url_stream_wrappers_hash();
+PHPAPI HashTable *_php_stream_get_url_stream_wrappers_hash(TSRMLS_D);
+#define php_stream_get_url_stream_wrappers_hash()      _php_stream_get_url_stream_wrappers_hash(TSRMLS_C)
 
 #endif
 
index 14d0bc827e09f862caeb03a84ba55dce36b2e7cd..1fe6c6b219a4ef362f1d3bc8ee4b48f0d777e414 100755 (executable)
@@ -96,6 +96,7 @@ FILE *fopencookie(void *cookie, const char *mode, COOKIE_IO_FUNCTIONS_T *funcs)
 # define PHP_STREAM_COOKIE_FUNCTIONS   stream_cookie_functions
 #endif
        
+/* Global wrapper hash, copied to FG(stream_wrappers) on registration of volatile wrapper */
 static HashTable url_stream_wrappers_hash;
 static int le_stream = FAILURE; /* true global */
 static int le_pstream = FAILURE; /* true global */
@@ -2335,6 +2336,7 @@ int php_shutdown_stream_wrappers(int module_number TSRMLS_DC)
        return SUCCESS;
 }
 
+/* API for registering GLOBAL wrappers */
 PHPAPI int php_register_url_stream_wrapper(char *protocol, php_stream_wrapper *wrapper TSRMLS_DC)
 {
        return zend_hash_add(&url_stream_wrappers_hash, protocol, strlen(protocol), wrapper, sizeof(*wrapper), NULL);
@@ -2344,6 +2346,20 @@ PHPAPI int php_unregister_url_stream_wrapper(char *protocol TSRMLS_DC)
 {
        return zend_hash_del(&url_stream_wrappers_hash, protocol, strlen(protocol));
 }
+
+/* API for registering VOLATILE wrappers */
+PHPAPI int php_register_url_stream_wrapper_volatile(char *protocol, php_stream_wrapper *wrapper TSRMLS_DC)
+{
+       if (!FG(stream_wrappers)) {
+               php_stream_wrapper tmpwrapper;
+
+               FG(stream_wrappers) = emalloc(sizeof(HashTable));
+               zend_hash_init(FG(stream_wrappers), 0, NULL, NULL, 1);
+               zend_hash_copy(FG(stream_wrappers), &url_stream_wrappers_hash, NULL, &tmpwrapper, sizeof(php_stream_wrapper));
+       }
+
+       return zend_hash_add(FG(stream_wrappers), protocol, strlen(protocol), wrapper, sizeof(*wrapper), NULL);
+}
 /* }}} */
 
 
@@ -2459,6 +2475,7 @@ static php_stream_wrapper php_plain_files_wrapper = {
 
 PHPAPI php_stream_wrapper *php_stream_locate_url_wrapper(const char *path, char **path_for_open, int options TSRMLS_DC)
 {
+       HashTable *wrapper_hash = (FG(stream_wrappers) ? FG(stream_wrappers) : &url_stream_wrappers_hash);
        php_stream_wrapper *wrapper = NULL;
        const char *p, *protocol = NULL;
        int n = 0;
@@ -2485,7 +2502,7 @@ PHPAPI php_stream_wrapper *php_stream_locate_url_wrapper(const char *path, char
        }
 
        if (protocol)   {
-               if (FAILURE == zend_hash_find(&url_stream_wrappers_hash, (char*)protocol, n, (void**)&wrapper)) {
+               if (FAILURE == zend_hash_find(wrapper_hash, (char*)protocol, n, (void**)&wrapper))      {
                        char wrapper_name[32];
 
                        if (options & REPORT_ERRORS) {
@@ -2911,9 +2928,9 @@ PHPAPI int php_stream_context_set_option(php_stream_context *context,
        return zend_hash_update(Z_ARRVAL_PP(wrapperhash), (char*)optionname, strlen(optionname)+1, (void**)&copied_val, sizeof(zval *), NULL);
 }
 
-PHPAPI HashTable *php_stream_get_url_stream_wrappers_hash()
+PHPAPI HashTable *_php_stream_get_url_stream_wrappers_hash(TSRMLS_D)
 {
-       return &url_stream_wrappers_hash;
+       return (FG(stream_wrappers) ? FG(stream_wrappers) : &url_stream_wrappers_hash);
 }
 
 /*
index 55130544c77180bbdd5b26c879909d00e8584491..d807555190c92dff05257d3407a0ffeaf4af9e8f 100644 (file)
@@ -51,7 +51,6 @@ static void stream_wrapper_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
 {
        struct php_user_stream_wrapper * uwrap = (struct php_user_stream_wrapper*)rsrc->ptr;
 
-       php_unregister_url_stream_wrapper(uwrap->protoname TSRMLS_CC);
        efree(uwrap->protoname);
        efree(uwrap->classname);
        efree(uwrap);
@@ -361,7 +360,7 @@ PHP_FUNCTION(stream_register_wrapper)
 #ifdef ZEND_ENGINE_2
                uwrap->ce = *(zend_class_entry**)uwrap->ce;
 #endif
-               if (php_register_url_stream_wrapper(protocol, &uwrap->wrapper TSRMLS_CC) == SUCCESS) {
+               if (php_register_url_stream_wrapper_volatile(protocol, &uwrap->wrapper TSRMLS_CC) == SUCCESS) {
                        RETURN_TRUE;
                }
        } else {