]> granicus.if.org Git - php/commitdiff
Introduce zend_stream_init_filename()
authorNikita Popov <nikita.ppv@gmail.com>
Tue, 16 Jul 2019 14:40:54 +0000 (16:40 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Tue, 16 Jul 2019 14:44:37 +0000 (16:44 +0200)
Avoid more ad-hoc initialization of zend_file_handle structures.

13 files changed:
Zend/zend_language_scanner.l
Zend/zend_stream.c
Zend/zend_stream.h
ext/opcache/ZendAccelerator.c
ext/opcache/zend_accelerator_module.c
ext/phar/phar_object.c
ext/readline/readline_cli.c
ext/standard/basic_functions.c
main/main.c
sapi/apache2handler/sapi_apache2.c
sapi/cgi/cgi_main.c
sapi/cli/php_cli_server.c
sapi/litespeed/lsapi_main.c

index 5824b6b7631ab4cc4a3aa361ddab04c09d776376..5526a0a0b352d50c34deb094ec67e9dbcba65a37 100644 (file)
@@ -659,11 +659,7 @@ zend_op_array *compile_filename(int type, zval *filename)
                ZVAL_STR(&tmp, zval_get_string(filename));
                filename = &tmp;
        }
-       file_handle.filename = Z_STRVAL_P(filename);
-       file_handle.free_filename = 0;
-       file_handle.type = ZEND_HANDLE_FILENAME;
-       file_handle.opened_path = NULL;
-       file_handle.handle.fp = NULL;
+       zend_stream_init_filename(&file_handle, Z_STRVAL_P(filename));
 
        retval = zend_compile_file(&file_handle, type);
        if (retval && file_handle.handle.stream.handle) {
@@ -789,10 +785,7 @@ int highlight_file(char *filename, zend_syntax_highlighter_ini *syntax_highlight
        zend_lex_state original_lex_state;
        zend_file_handle file_handle;
 
-       file_handle.type = ZEND_HANDLE_FILENAME;
-       file_handle.filename = filename;
-       file_handle.free_filename = 0;
-       file_handle.opened_path = NULL;
+       zend_stream_init_filename(&file_handle, filename);
        zend_save_lexical_state(&original_lex_state);
        if (open_file_for_scanning(&file_handle)==FAILURE) {
                zend_message_dispatcher(ZMSG_FAILED_HIGHLIGHT_FOPEN, filename);
index fc0523ee965747d7f4289bd65ff060c42d098831..08a1db2583a809656720781e95a4483be41c3838 100644 (file)
@@ -101,6 +101,12 @@ ZEND_API void zend_stream_init_fp(zend_file_handle *handle, FILE *fp, const char
        handle->filename = filename;
 }
 
+ZEND_API void zend_stream_init_filename(zend_file_handle *handle, const char *filename) {
+       memset(handle, 0, sizeof(zend_file_handle));
+       handle->type = ZEND_HANDLE_FILENAME;
+       handle->filename = filename;
+}
+
 ZEND_API int zend_stream_open(const char *filename, zend_file_handle *handle) /* {{{ */
 {
        if (zend_stream_open_function) {
index 1438a51dea581b1322c90b7c2a675621546dd043..18b07d6b3cafb78231cb3107a6bcdac2b2eae1c7 100644 (file)
@@ -70,6 +70,7 @@ typedef struct _zend_file_handle {
 
 BEGIN_EXTERN_C()
 ZEND_API void zend_stream_init_fp(zend_file_handle *handle, FILE *fp, const char *filename);
+ZEND_API void zend_stream_init_filename(zend_file_handle *handle, const char *filename);
 ZEND_API int zend_stream_open(const char *filename, zend_file_handle *handle);
 ZEND_API int zend_stream_fixup(zend_file_handle *file_handle, char **buf, size_t *len);
 ZEND_API void zend_file_handle_dtor(zend_file_handle *fh);
index 81d579d9a859f944db570972f0c7dae864ee4994..cc576376424fe08933f62673340ff850cc0d2143 100644 (file)
@@ -1071,8 +1071,7 @@ static inline int do_validate_timestamps(zend_persistent_script *persistent_scri
                file_handle->opened_path = NULL;
        }
 
-       ps_handle.type = ZEND_HANDLE_FILENAME;
-       ps_handle.filename = ZSTR_VAL(persistent_script->script.filename);
+       zend_stream_init_filename(&ps_handle, ZSTR_VAL(persistent_script->script.filename));
        ps_handle.opened_path = persistent_script->script.filename;
 
        if (zend_get_file_handle_timestamp(&ps_handle, NULL) == persistent_script->timestamp) {
@@ -1297,9 +1296,7 @@ int zend_accel_invalidate(const char *filename, size_t filename_len, zend_bool f
        persistent_script = zend_accel_hash_find(&ZCSG(hash), realpath);
        if (persistent_script && !persistent_script->corrupted) {
                zend_file_handle file_handle;
-
-               file_handle.type = ZEND_HANDLE_FILENAME;
-               file_handle.filename = ZSTR_VAL(realpath);
+               zend_stream_init_filename(&file_handle, ZSTR_VAL(realpath));
                file_handle.opened_path = realpath;
 
                if (force ||
@@ -2198,10 +2195,8 @@ static int persistent_stream_open_function(const char *filename, zend_file_handl
                     ZCG(cache_opline) == EG(current_execute_data)->opline)) {
 
                        /* we are in include_once or FastCGI request */
-                       handle->filename = (char*)filename;
-                       handle->free_filename = 0;
+                       zend_stream_init_filename(handle, (char*) filename);
                        handle->opened_path = zend_string_copy(ZCG(cache_persistent_script)->script.filename);
-                       handle->type = ZEND_HANDLE_FILENAME;
                        return SUCCESS;
                }
                ZCG(cache_opline) = NULL;
@@ -4139,12 +4134,7 @@ static int accel_preload(const char *config)
        orig_map_ptr_last = CG(map_ptr_last);
 
        /* Compile and execute proloading script */
-       memset(&file_handle, 0, sizeof(file_handle));
-       file_handle.filename = (char*)config;
-       file_handle.free_filename = 0;
-       file_handle.type = ZEND_HANDLE_FILENAME;
-       file_handle.opened_path = NULL;
-       file_handle.handle.fp = NULL;
+       zend_stream_init_filename(&file_handle, (char *) config);
 
        preload_scripts = emalloc(sizeof(HashTable));
        zend_hash_init(preload_scripts, 0, NULL, NULL, 0);
index a8a1fc99a6a3088d8c1fd612014dc4f0e8197ade..bcbc9277031a4d203b7da5332ceedd4c412a3aa0 100644 (file)
@@ -332,12 +332,9 @@ static int filename_is_in_cache(zend_string *filename)
        if (key != NULL) {
                zend_persistent_script *persistent_script = zend_accel_hash_str_find(&ZCSG(hash), key, key_length);
                if (persistent_script && !persistent_script->corrupted) {
-                       zend_file_handle handle = {{0}, NULL, NULL, 0, 0};
-
-                       handle.filename = ZSTR_VAL(filename);
-                       handle.type = ZEND_HANDLE_FILENAME;
-
                        if (ZCG(accel_directives).validate_timestamps) {
+                               zend_file_handle handle;
+                               zend_stream_init_filename(&handle, ZSTR_VAL(filename));
                                return validate_timestamp_and_record_ex(persistent_script, &handle) == SUCCESS;
                        }
 
@@ -849,10 +846,7 @@ static ZEND_FUNCTION(opcache_compile_file)
                return;
        }
 
-       handle.filename = script_name;
-       handle.free_filename = 0;
-       handle.opened_path = NULL;
-       handle.type = ZEND_HANDLE_FILENAME;
+       zend_stream_init_filename(&handle, script_name);
 
        orig_execute_data = EG(current_execute_data);
        orig_compiler_options = CG(compiler_options);
index 783b7f77f86614a66da708d73e28e973856fb9ab..3478507499d90798fdf3533c849c8ba20eabe98a 100644 (file)
@@ -219,10 +219,7 @@ static int phar_file_action(phar_archive_data *phar, phar_entry_info *info, char
                                name_len = spprintf(&name, 4096, "phar://%s/%s", arch, entry);
                        }
 
-                       file_handle.type = ZEND_HANDLE_FILENAME;
-                       file_handle.filename = name;
-                       file_handle.opened_path = NULL;
-                       file_handle.free_filename = 0;
+                       zend_stream_init_filename(&file_handle, name);
 
                        PHAR_G(cwd) = NULL;
                        PHAR_G(cwd_len) = 0;
index 956386c532ec343daed39ccf0168c8de7dfde4ea..c533fc27432912a98af1bae59eba3804a54e3205 100644 (file)
@@ -592,17 +592,9 @@ static int readline_shell_run(void) /* {{{ */
        int history_lines_to_write = 0;
 
        if (PG(auto_prepend_file) && PG(auto_prepend_file)[0]) {
-               zend_file_handle *prepend_file_p;
                zend_file_handle prepend_file;
-
-               memset(&prepend_file, 0, sizeof(prepend_file));
-               prepend_file.filename = PG(auto_prepend_file);
-               prepend_file.opened_path = NULL;
-               prepend_file.free_filename = 0;
-               prepend_file.type = ZEND_HANDLE_FILENAME;
-               prepend_file_p = &prepend_file;
-
-               zend_execute_scripts(ZEND_REQUIRE, NULL, 1, prepend_file_p);
+               zend_stream_init_filename(&prepend_file, PG(auto_prepend_file));
+               zend_execute_scripts(ZEND_REQUIRE, NULL, 1, &prepend_file);
        }
 
 #ifndef PHP_WIN32
index 6bbd24f6cc284419a4e120750523d6f80a3e5486..cfe59fb8ec7c472f609ff5a69b138c994cc426bd 100644 (file)
@@ -5315,11 +5315,7 @@ PHP_FUNCTION(php_strip_whitespace)
 
        php_output_start_default();
 
-       memset(&file_handle, 0, sizeof(file_handle));
-       file_handle.type = ZEND_HANDLE_FILENAME;
-       file_handle.filename = filename;
-       file_handle.free_filename = 0;
-       file_handle.opened_path = NULL;
+       zend_stream_init_filename(&file_handle, filename);
        zend_save_lexical_state(&original_lex_state);
        if (open_file_for_scanning(&file_handle) == FAILURE) {
                zend_restore_lexical_state(&original_lex_state);
@@ -6082,9 +6078,7 @@ PHP_FUNCTION(parse_ini_file)
        }
 
        /* Setup filehandle */
-       memset(&fh, 0, sizeof(fh));
-       fh.filename = filename;
-       fh.type = ZEND_HANDLE_FILENAME;
+       zend_stream_init_filename(&fh, filename);
 
        array_init(return_value);
        if (zend_parse_ini_file(&fh, 0, (int)scanner_mode, ini_parser_cb, return_value) == FAILURE) {
index c9d37ac2e00f00b7401af114fde2c1991d8a34a2..90cf190f431baa0251a3f26f86dad7d79bce19c0 100644 (file)
@@ -2517,7 +2517,7 @@ void php_module_shutdown(void)
 PHPAPI int php_execute_script(zend_file_handle *primary_file)
 {
        zend_file_handle *prepend_file_p, *append_file_p;
-       zend_file_handle prepend_file = {{0}, NULL, NULL, 0, 0}, append_file = {{0}, NULL, NULL, 0, 0};
+       zend_file_handle prepend_file, append_file;
 #if HAVE_BROKEN_GETCWD
        volatile int old_cwd_fd = -1;
 #else
@@ -2569,20 +2569,14 @@ PHPAPI int php_execute_script(zend_file_handle *primary_file)
                }
 
                if (PG(auto_prepend_file) && PG(auto_prepend_file)[0]) {
-                       prepend_file.filename = PG(auto_prepend_file);
-                       prepend_file.opened_path = NULL;
-                       prepend_file.free_filename = 0;
-                       prepend_file.type = ZEND_HANDLE_FILENAME;
+                       zend_stream_init_filename(&prepend_file, PG(auto_prepend_file));
                        prepend_file_p = &prepend_file;
                } else {
                        prepend_file_p = NULL;
                }
 
                if (PG(auto_append_file) && PG(auto_append_file)[0]) {
-                       append_file.filename = PG(auto_append_file);
-                       append_file.opened_path = NULL;
-                       append_file.free_filename = 0;
-                       append_file.type = ZEND_HANDLE_FILENAME;
+                       zend_stream_init_filename(&append_file, PG(auto_append_file));
                        append_file_p = &append_file;
                } else {
                        append_file_p = NULL;
index 193420eca42509214042b4b2ce1bbdf2fc044d40..1cc5670eaab1822bf9ea6238f9d56bbc6f4b13cf 100644 (file)
@@ -694,11 +694,7 @@ zend_first_try {
                highlight_file((char *)r->filename, &syntax_highlighter_ini);
        } else {
                zend_file_handle zfd;
-
-               zfd.type = ZEND_HANDLE_FILENAME;
-               zfd.filename = (char *) r->filename;
-               zfd.free_filename = 0;
-               zfd.opened_path = NULL;
+               zend_stream_init_filename(&zfd, (char *) r->filename);
 
                if (!parent_req) {
                        php_execute_script(&zfd);
index 1068930c39ff75bd8022279b4c69b62fba47e871..81de65351fa71740e8e17b27c9ada805630ddfc8 100644 (file)
@@ -2476,11 +2476,7 @@ parent_loop_end:
                                we need in the environment.
                        */
                        if (SG(request_info).path_translated || cgi || fastcgi) {
-                               file_handle.type = ZEND_HANDLE_FILENAME;
-                               file_handle.filename = SG(request_info).path_translated;
-                               file_handle.handle.fp = NULL;
-                               file_handle.opened_path = NULL;
-                               file_handle.free_filename = 0;
+                               zend_stream_init_filename(&file_handle, SG(request_info).path_translated);
                        } else {
                                zend_stream_init_fp(&file_handle, stdin, "Standard input code");
                        }
index 40fad8c10bdad1fcb3e0a05fbcbb99c3d87ea172..f00d8dee54102243fd0869a758fcc906d562fe81 100644 (file)
@@ -2072,11 +2072,7 @@ static int php_cli_server_dispatch_script(php_cli_server *server, php_cli_server
        }
        {
                zend_file_handle zfd;
-               zfd.type = ZEND_HANDLE_FILENAME;
-               zfd.filename = SG(request_info).path_translated;
-               zfd.handle.fp = NULL;
-               zfd.free_filename = 0;
-               zfd.opened_path = NULL;
+               zend_stream_init_filename(&zfd, SG(request_info).path_translated);
                zend_try {
                        php_execute_script(&zfd);
                } zend_end_try();
@@ -2197,11 +2193,7 @@ static int php_cli_server_dispatch_router(php_cli_server *server, php_cli_server
        old_cwd[0] = '\0';
        php_ignore_value(VCWD_GETCWD(old_cwd, MAXPATHLEN - 1));
 
-       zfd.type = ZEND_HANDLE_FILENAME;
-       zfd.filename = server->router;
-       zfd.handle.fp = NULL;
-       zfd.free_filename = 0;
-       zfd.opened_path = NULL;
+       zend_stream_init_filename(&zfd, server->router);
 
        zend_try {
                zval retval;
index bec0eacb81ce3ba4f0585ab73cdcee774036b814..baff98df1e2d4570563c25f183fec76fcd007f71 100644 (file)
@@ -657,11 +657,7 @@ static int lsapi_execute_script( zend_file_handle * file_handle)
 {
     char *p;
     int len;
-    file_handle->type = ZEND_HANDLE_FILENAME;
-    file_handle->handle.fd = 0;
-    file_handle->filename = SG(request_info).path_translated;
-    file_handle->free_filename = 0;
-    file_handle->opened_path = NULL;
+       zend_stream_init_filename(file_handle, SG(request_info).path_translated);
 
     p = argv0;
     *p++ = ':';