diff options
author | Dmitry Stogov <dmitry@zend.com> | 2021-03-16 20:31:36 +0300 |
---|---|---|
committer | Dmitry Stogov <dmitry@zend.com> | 2021-03-16 20:31:36 +0300 |
commit | c732ab400af92c54eee47c487a56009f1d79dd5d (patch) | |
tree | 083d31748450932114a7667aae9235cde030efcb /sapi | |
parent | 9bbeb0555b6b842ebd44e08510ff3f3226237544 (diff) | |
download | php-git-c732ab400af92c54eee47c487a56009f1d79dd5d.tar.gz |
Change Zend Stream API to use zend_string* instead of char*.
This allows to eliminate re-calculation of string lenght and hash value.
See the detailed list of changes in UPGRADING.INTERNALS.
Diffstat (limited to 'sapi')
-rw-r--r-- | sapi/apache2handler/sapi_apache2.c | 2 | ||||
-rw-r--r-- | sapi/cgi/cgi_main.c | 45 | ||||
-rw-r--r-- | sapi/cli/php_cli.c | 31 | ||||
-rw-r--r-- | sapi/cli/php_cli_server.c | 5 | ||||
-rw-r--r-- | sapi/fuzzer/fuzzer-sapi.c | 2 | ||||
-rw-r--r-- | sapi/litespeed/lsapi_main.c | 22 | ||||
-rw-r--r-- | sapi/phpdbg/phpdbg_list.c | 17 | ||||
-rw-r--r-- | sapi/phpdbg/phpdbg_prompt.c | 5 |
8 files changed, 72 insertions, 57 deletions
diff --git a/sapi/apache2handler/sapi_apache2.c b/sapi/apache2handler/sapi_apache2.c index 2266b46e58..049f1b7d35 100644 --- a/sapi/apache2handler/sapi_apache2.c +++ b/sapi/apache2handler/sapi_apache2.c @@ -699,12 +699,14 @@ zend_first_try { } else { zend_file_handle zfd; zend_stream_init_filename(&zfd, (char *) r->filename); + zfd.primary_script = 1; if (!parent_req) { php_execute_script(&zfd); } else { zend_execute_scripts(ZEND_INCLUDE, NULL, 1, &zfd); } + zend_destroy_file_handle(&zfd); apr_table_set(r->notes, "mod_php_memory_usage", apr_psprintf(ctx->r->pool, "%" APR_SIZE_T_FMT, zend_memory_peak_usage(1))); diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c index acde8c22cb..8673ec47df 100644 --- a/sapi/cgi/cgi_main.c +++ b/sapi/cgi/cgi_main.c @@ -2450,17 +2450,6 @@ parent_loop_end: } } /* end !cgi && !fastcgi */ - /* - we never take stdin if we're (f)cgi, always - rely on the web server giving us the info - we need in the environment. - */ - if (SG(request_info).path_translated || cgi || fastcgi) { - zend_stream_init_filename(&file_handle, SG(request_info).path_translated); - } else { - zend_stream_init_fp(&file_handle, stdin, "Standard input code"); - } - /* request startup only after we've done all we can to * get path_translated */ if (php_request_startup() == FAILURE) { @@ -2520,6 +2509,10 @@ parent_loop_end: free(bindpath); return FAILURE; } + } else { + /* we never take stdin if we're (f)cgi */ + zend_stream_init_fp(&file_handle, stdin, "Standard input code"); + file_handle.primary_script = 1; } if (CGIG(check_shebang_line)) { @@ -2534,9 +2527,9 @@ parent_loop_end: PG(during_request_startup) = 0; exit_status = php_lint_script(&file_handle); if (exit_status == SUCCESS) { - zend_printf("No syntax errors detected in %s\n", file_handle.filename); + zend_printf("No syntax errors detected in %s\n", ZSTR_VAL(file_handle.filename)); } else { - zend_printf("Errors parsing %s\n", file_handle.filename); + zend_printf("Errors parsing %s\n", ZSTR_VAL(file_handle.filename)); } break; case PHP_MODE_STRIP: @@ -2557,22 +2550,22 @@ parent_loop_end: } fastcgi_request_done: - { - if (SG(request_info).path_translated) { - efree(SG(request_info).path_translated); - SG(request_info).path_translated = NULL; - } + zend_destroy_file_handle(&file_handle); - php_request_shutdown((void *) 0); + if (SG(request_info).path_translated) { + efree(SG(request_info).path_translated); + SG(request_info).path_translated = NULL; + } - if (exit_status == 0) { - exit_status = EG(exit_status); - } + php_request_shutdown((void *) 0); - if (free_query_string && SG(request_info).query_string) { - free(SG(request_info).query_string); - SG(request_info).query_string = NULL; - } + if (exit_status == 0) { + exit_status = EG(exit_status); + } + + if (free_query_string && SG(request_info).query_string) { + free(SG(request_info).query_string); + SG(request_info).query_string = NULL; } if (!fastcgi) { diff --git a/sapi/cli/php_cli.c b/sapi/cli/php_cli.c index 472ce013ea..835582df88 100644 --- a/sapi/cli/php_cli.c +++ b/sapi/cli/php_cli.c @@ -587,6 +587,7 @@ static int cli_seek_file_begin(zend_file_handle *file_handle, char *script_file) } zend_stream_init_fp(file_handle, fp, script_file); + file_handle->primary_script = 1; return SUCCESS; } /* }}} */ @@ -620,6 +621,8 @@ static int do_cli(int argc, char **argv) /* {{{ */ int num_repeats = 1; pid_t pid = getpid(); + file_handle.filename = NULL; + zend_try { CG(in_compilation) = 0; /* not initialized but needed for several options */ @@ -907,28 +910,32 @@ do_repeat: translated_path = strdup(real_path); } script_filename = script_file; + php_self = script_file; } } else { /* We could handle PHP_MODE_PROCESS_STDIN in a different manner */ /* here but this would make things only more complicated. And it */ /* is consistent with the way -R works where the stdin file handle*/ /* is also accessible. */ - zend_stream_init_fp(&file_handle, stdin, "Standard input code"); + php_self = "Standard input code"; + if (behavior < PHP_MODE_CLI_DIRECT + && (!interactive || PHP_MODE_STANDARD != PHP_MODE_STANDARD)) { + zend_stream_init_fp(&file_handle, stdin, php_self); + file_handle.primary_script = 1; + } } - php_self = (char*)file_handle.filename; /* before registering argv to module exchange the *new* argv[0] */ /* we can achieve this without allocating more memory */ SG(request_info).argc=argc-php_optind+1; arg_excp = argv+php_optind-1; arg_free = argv[php_optind-1]; - SG(request_info).path_translated = translated_path? translated_path: (char*)file_handle.filename; - argv[php_optind-1] = (char*)file_handle.filename; + SG(request_info).path_translated = translated_path ? translated_path : php_self; + argv[php_optind-1] = php_self; SG(request_info).argv=argv+php_optind-1; if (php_request_startup()==FAILURE) { *arg_excp = arg_free; - fclose(file_handle.handle.fp); PUTS("Could not startup.\n"); goto err; } @@ -954,7 +961,7 @@ do_repeat: PG(during_request_startup) = 0; switch (behavior) { case PHP_MODE_STANDARD: - if (strcmp(file_handle.filename, "Standard input code")) { + if (script_file) { cli_register_file_handles(/* no_close */ PHP_DEBUG || num_repeats > 1); } @@ -967,9 +974,9 @@ do_repeat: case PHP_MODE_LINT: EG(exit_status) = php_lint_script(&file_handle); if (EG(exit_status) == SUCCESS) { - zend_printf("No syntax errors detected in %s\n", file_handle.filename); + zend_printf("No syntax errors detected in %s\n", php_self); } else { - zend_printf("Errors parsing %s\n", file_handle.filename); + zend_printf("Errors parsing %s\n", php_self); } break; case PHP_MODE_STRIP: @@ -1000,6 +1007,11 @@ do_repeat: size_t len, index = 0; zval argn, argi; + if (!exec_run && script_file) { + zend_string_release_ex(file_handle.filename, 0); + file_handle.filename = NULL; + } + cli_register_file_handles(/* no_close */ PHP_DEBUG || num_repeats > 1); if (exec_begin) { @@ -1121,6 +1133,9 @@ do_repeat: } zend_end_try(); out: + if (file_handle.filename) { + zend_destroy_file_handle(&file_handle); + } if (request_started) { php_request_shutdown((void *) 0); } diff --git a/sapi/cli/php_cli_server.c b/sapi/cli/php_cli_server.c index 6d28b472d7..6511e08c9a 100644 --- a/sapi/cli/php_cli_server.c +++ b/sapi/cli/php_cli_server.c @@ -2015,9 +2015,11 @@ static int php_cli_server_dispatch_script(php_cli_server *server, php_cli_server { zend_file_handle zfd; zend_stream_init_filename(&zfd, SG(request_info).path_translated); + zfd.primary_script = 1; zend_try { php_execute_script(&zfd); } zend_end_try(); + zend_destroy_file_handle(&zfd); } php_cli_server_log_response(client, SG(sapi_headers).http_response_code, NULL); @@ -2136,6 +2138,7 @@ static int php_cli_server_dispatch_router(php_cli_server *server, php_cli_server php_ignore_value(VCWD_GETCWD(old_cwd, MAXPATHLEN - 1)); zend_stream_init_filename(&zfd, server->router); + zfd.primary_script = 1; zend_try { zval retval; @@ -2149,6 +2152,8 @@ static int php_cli_server_dispatch_router(php_cli_server *server, php_cli_server } } zend_end_try(); + zend_destroy_file_handle(&zfd); + if (old_cwd[0] != '\0') { php_ignore_value(VCWD_CHDIR(old_cwd)); } diff --git a/sapi/fuzzer/fuzzer-sapi.c b/sapi/fuzzer/fuzzer-sapi.c index f4c21f7f1c..60665f62ab 100644 --- a/sapi/fuzzer/fuzzer-sapi.c +++ b/sapi/fuzzer/fuzzer-sapi.c @@ -250,6 +250,7 @@ int fuzzer_do_request_from_buffer( zend_first_try { zend_file_handle file_handle; zend_stream_init_filename(&file_handle, filename); + file_handle.primary_script = 1; file_handle.buf = estrndup(data, data_len); file_handle.len = data_len; @@ -261,6 +262,7 @@ int fuzzer_do_request_from_buffer( destroy_op_array(op_array); efree(op_array); } + zend_destroy_file_handle(&file_handle); } zend_end_try(); CG(compiled_filename) = NULL; /* ??? */ diff --git a/sapi/litespeed/lsapi_main.c b/sapi/litespeed/lsapi_main.c index 5b3f20ebfb..2ab2336aab 100644 --- a/sapi/litespeed/lsapi_main.c +++ b/sapi/litespeed/lsapi_main.c @@ -643,11 +643,13 @@ static void init_request_info( void ) php_handle_auth_data(pAuth); } -static int lsapi_execute_script( zend_file_handle * file_handle) +static int lsapi_execute_script(void) { + zend_file_handle file_handle; char *p; int len; - zend_stream_init_filename(file_handle, SG(request_info).path_translated); + zend_stream_init_filename(&file_handle, SG(request_info).path_translated); + file_handle->primary_script = 1; p = argv0; *p++ = ':'; @@ -658,7 +660,8 @@ static int lsapi_execute_script( zend_file_handle * file_handle) len = 0; memccpy( p, SG(request_info).path_translated + len, 0, 46 ); - php_execute_script(file_handle); + php_execute_script(&file_handle); + zend_destroy_file_handle(&file_handle); return 0; } @@ -740,8 +743,6 @@ static int lsapi_module_main(int show_source) { struct sigaction act; int sa_rc; - zend_file_handle file_handle; - memset(&file_handle, 0, sizeof(file_handle)); if (php_request_startup() == FAILURE ) { return -1; } @@ -767,7 +768,7 @@ static int lsapi_module_main(int show_source) php_get_highlight_struct(&syntax_highlighter_ini); highlight_file(SG(request_info).path_translated, &syntax_highlighter_ini); } else { - lsapi_execute_script( &file_handle); + lsapi_execute_script(); } zend_try { php_request_shutdown(NULL); @@ -1308,8 +1309,9 @@ static int cli_main( int argc, char * argv[] ) } if ( ret == -1 ) { if ( *p ) { - zend_file_handle file_handle; + zend_file_handle file_handle; zend_stream_init_fp(&file_handle, VCWD_FOPEN(*p, "rb"), NULL); + file_handle.primary_script = 1; if ( file_handle.handle.fp ) { script_filename = *p; @@ -1329,8 +1331,7 @@ static int cli_main( int argc, char * argv[] ) php_get_highlight_struct(&syntax_highlighter_ini); highlight_file(SG(request_info).path_translated, &syntax_highlighter_ini); } else if (source_highlight == 2) { - file_handle.filename = *p; - file_handle.free_filename = 0; + file_handle.filename = zend_string_init(*p, strlen(*p), 0); file_handle.opened_path = NULL; ret = php_lint_script(&file_handle); if (ret==SUCCESS) { @@ -1340,8 +1341,7 @@ static int cli_main( int argc, char * argv[] ) } } else { - file_handle.filename = *p; - file_handle.free_filename = 0; + file_handle.filename = zend_string_init(*p, strlen(*p), 0); file_handle.opened_path = NULL; php_execute_script(&file_handle); diff --git a/sapi/phpdbg/phpdbg_list.c b/sapi/phpdbg/phpdbg_list.c index 0967e22f65..73f88d46a1 100644 --- a/sapi/phpdbg/phpdbg_list.c +++ b/sapi/phpdbg/phpdbg_list.c @@ -241,9 +241,9 @@ zend_op_array *phpdbg_compile_file(zend_file_handle *file, int type) { * as it may invalidate the file handle. */ if (zend_stream_fixup(file, &bufptr, &len) == FAILURE) { if (type == ZEND_REQUIRE) { - zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, file->filename); + zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, ZSTR_VAL(file->filename)); } else { - zend_message_dispatcher(ZMSG_FAILED_INCLUDE_FOPEN, file->filename); + zend_message_dispatcher(ZMSG_FAILED_INCLUDE_FOPEN, ZSTR_VAL(file->filename)); } return NULL; } @@ -279,22 +279,19 @@ zend_op_array *phpdbg_compile_file(zend_file_handle *file, int type) { } zend_op_array *phpdbg_init_compile_file(zend_file_handle *file, int type) { - char *filename = (char *)(file->opened_path ? ZSTR_VAL(file->opened_path) : file->filename); + zend_string *filename = file->opened_path ? file->opened_path : file->filename; char resolved_path_buf[MAXPATHLEN]; zend_op_array *op_array; phpdbg_file_source *dataptr; - if (VCWD_REALPATH(filename, resolved_path_buf)) { - filename = resolved_path_buf; + if (VCWD_REALPATH(ZSTR_VAL(filename), resolved_path_buf)) { + filename = zend_string_init(resolved_path_buf, strlen(resolved_path_buf), 0); if (file->opened_path) { zend_string_release(file->opened_path); - file->opened_path = zend_string_init(filename, strlen(filename), 0); + file->opened_path = filename; } else { - if (file->free_filename) { - efree((char *) file->filename); - } - file->free_filename = 0; + zend_string_release(file->filename); file->filename = filename; } } diff --git a/sapi/phpdbg/phpdbg_prompt.c b/sapi/phpdbg/phpdbg_prompt.c index 13026978bf..acdae17ce3 100644 --- a/sapi/phpdbg/phpdbg_prompt.c +++ b/sapi/phpdbg/phpdbg_prompt.c @@ -566,7 +566,8 @@ int phpdbg_compile(void) /* {{{ */ return FAILURE; } - if (php_stream_open_for_zend_ex(PHPDBG_G(exec), &fh, USE_PATH|STREAM_OPEN_FOR_INCLUDE) == SUCCESS && zend_stream_fixup(&fh, &buf, &len) == SUCCESS) { + zend_stream_init_filename(&fh, PHPDBG_G(exec)); + if (php_stream_open_for_zend_ex(&fh, USE_PATH|STREAM_OPEN_FOR_INCLUDE) == SUCCESS && zend_stream_fixup(&fh, &buf, &len) == SUCCESS) { CG(skip_shebang) = 1; PHPDBG_G(ops) = zend_compile_file(&fh, ZEND_INCLUDE); zend_destroy_file_handle(&fh); @@ -581,7 +582,7 @@ int phpdbg_compile(void) /* {{{ */ } else { phpdbg_error("compile", "type=\"openfailure\" context=\"%s\"", "Could not open file %s", PHPDBG_G(exec)); } - + zend_destroy_file_handle(&fh); return FAILURE; } /* }}} */ |