From: Dmitry Stogov Date: Thu, 5 Mar 2015 15:18:39 +0000 (+0300) Subject: Avoid repeatable strlen() calls X-Git-Tag: PRE_PHP7_NSAPI_REMOVAL~802 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6d065a8545291a8048ef307476c4358922ca3a61;p=php Avoid repeatable strlen() calls --- diff --git a/Zend/zend_execute.h b/Zend/zend_execute.h index 4298f6bdd6..d33735291a 100644 --- a/Zend/zend_execute.h +++ b/Zend/zend_execute.h @@ -221,6 +221,7 @@ static zend_always_inline void zend_vm_stack_free_call_frame(zend_execute_data * ZEND_API const char *get_active_class_name(const char **space); ZEND_API const char *get_active_function_name(void); ZEND_API const char *zend_get_executed_filename(void); +ZEND_API zend_string *zend_get_executed_filename_ex(void); ZEND_API uint zend_get_executed_lineno(void); ZEND_API zend_bool zend_is_executing(void); diff --git a/Zend/zend_execute_API.c b/Zend/zend_execute_API.c index f52d9f33ce..3c1bd87bac 100644 --- a/Zend/zend_execute_API.c +++ b/Zend/zend_execute_API.c @@ -461,6 +461,21 @@ ZEND_API const char *zend_get_executed_filename(void) /* {{{ */ } /* }}} */ +ZEND_API zend_string *zend_get_executed_filename_ex(void) /* {{{ */ +{ + zend_execute_data *ex = EG(current_execute_data); + + while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) { + ex = ex->prev_execute_data; + } + if (ex) { + return ex->func->op_array.filename; + } else { + return NULL; + } +} +/* }}} */ + ZEND_API uint zend_get_executed_lineno(void) /* {{{ */ { zend_execute_data *ex = EG(current_execute_data); diff --git a/ext/opcache/ZendAccelerator.c b/ext/opcache/ZendAccelerator.c index a1e7d9c42d..8b56c86788 100644 --- a/ext/opcache/ZendAccelerator.c +++ b/ext/opcache/ZendAccelerator.c @@ -927,8 +927,8 @@ char *accel_make_persistent_key_ex(zend_file_handle *file_handle, int path_lengt !is_stream_path(file_handle->filename)) { char *include_path = NULL; int include_path_len = 0; - const char *parent_script = NULL; - int parent_script_len = 0; + zend_string *parent_script = NULL; + size_t parent_script_len = 0; int cur_len = 0; int cwd_len; char *cwd; @@ -990,11 +990,10 @@ char *accel_make_persistent_key_ex(zend_file_handle *file_handle, int path_lengt in include path too. */ if (EG(current_execute_data) && - (parent_script = zend_get_executed_filename()) != NULL && - parent_script[0] != '[') { + (parent_script = zend_get_executed_filename_ex()) != NULL) { - parent_script_len = strlen(parent_script); - while ((--parent_script_len > 0) && !IS_SLASH(parent_script[parent_script_len])); + parent_script_len = parent_script->len; + while ((--parent_script_len > 0) && !IS_SLASH(parent_script->val[parent_script_len])); } /* Calculate key length */ @@ -1022,7 +1021,7 @@ char *accel_make_persistent_key_ex(zend_file_handle *file_handle, int path_lengt cur_len = cwd_len + 1 + path_length + 1; if (parent_script_len) { - memcpy(ZCG(key) + cur_len, parent_script, parent_script_len); + memcpy(ZCG(key) + cur_len, parent_script->val, parent_script_len); cur_len += parent_script_len; ZCG(key)[cur_len] = ':'; cur_len++; diff --git a/main/fopen_wrappers.c b/main/fopen_wrappers.c index c516481298..396b0c51a0 100644 --- a/main/fopen_wrappers.c +++ b/main/fopen_wrappers.c @@ -493,6 +493,7 @@ PHPAPI zend_string *php_resolve_path(const char *filename, int filename_length, const char *ptr, *end, *p; const char *actual_path; php_stream_wrapper *wrapper; + zend_string *exec_filename; if (!filename || CHECK_NULL_PATH(filename, filename_length)) { return NULL; @@ -580,13 +581,13 @@ PHPAPI zend_string *php_resolve_path(const char *filename, int filename_length, /* check in calling scripts' current working directory as a fall back case */ - if (zend_is_executing()) { - const char *exec_fname = zend_get_executed_filename(); - int exec_fname_length = (int)strlen(exec_fname); + if (zend_is_executing() && + (exec_filename = zend_get_executed_filename_ex()) != NULL) { + const char *exec_fname = exec_filename->val; + size_t exec_fname_length = exec_filename->len; while ((--exec_fname_length >= 0) && !IS_SLASH(exec_fname[exec_fname_length])); - if (exec_fname && exec_fname[0] != '[' && - exec_fname_length > 0 && + if (exec_fname_length > 0 && exec_fname_length + 1 + filename_length + 1 < MAXPATHLEN) { memcpy(trypath, exec_fname, exec_fname_length + 1); memcpy(trypath+exec_fname_length + 1, filename, filename_length+1); @@ -627,12 +628,10 @@ PHPAPI zend_string *php_resolve_path(const char *filename, int filename_length, PHPAPI FILE *php_fopen_with_path(const char *filename, const char *mode, const char *path, zend_string **opened_path) { char *pathbuf, *ptr, *end; - const char *exec_fname; char trypath[MAXPATHLEN]; FILE *fp; - int path_length; int filename_length; - int exec_fname_length; + zend_string *exec_filename; if (opened_path) { *opened_path = NULL; @@ -660,16 +659,18 @@ PHPAPI FILE *php_fopen_with_path(const char *filename, const char *mode, const c /* append the calling scripts' current working directory * as a fall back case */ - if (zend_is_executing()) { - exec_fname = zend_get_executed_filename(); - exec_fname_length = (int)strlen(exec_fname); - path_length = (int)strlen(path); + if (zend_is_executing() && + (exec_filename = zend_get_executed_filename_ex()) != NULL) { + const char *exec_fname = exec_filename->val; + size_t exec_fname_length = exec_filename->len; while ((--exec_fname_length >= 0) && !IS_SLASH(exec_fname[exec_fname_length])); if ((exec_fname && exec_fname[0] == '[') || exec_fname_length <= 0) { /* [no active file] or no path */ pathbuf = estrdup(path); } else { + size_t path_length = strlen(path); + pathbuf = (char *) emalloc(exec_fname_length + path_length + 1 + 1); memcpy(pathbuf, path, path_length); pathbuf[path_length] = DEFAULT_DIR_SEPARATOR; diff --git a/main/streams/plain_wrapper.c b/main/streams/plain_wrapper.c index 83bd059c34..6af8ffa964 100644 --- a/main/streams/plain_wrapper.c +++ b/main/streams/plain_wrapper.c @@ -1390,12 +1390,10 @@ PHPAPI php_stream *_php_stream_fopen_with_path(const char *filename, const char /* code ripped off from fopen_wrappers.c */ char *pathbuf, *end; const char *ptr; - const char *exec_fname; char trypath[MAXPATHLEN]; php_stream *stream; - int path_length; int filename_length; - int exec_fname_length; + zend_string *exec_filename; if (opened_path) { *opened_path = NULL; @@ -1471,17 +1469,18 @@ not_relative_path: /* append the calling scripts' current working directory * as a fall back case */ - if (zend_is_executing()) { - exec_fname = zend_get_executed_filename(); - exec_fname_length = (int)strlen(exec_fname); - path_length = (int)strlen(path); + if (zend_is_executing() && + (exec_filename = zend_get_executed_filename_ex()) != NULL) { + const char *exec_fname = exec_filename->val; + size_t exec_fname_length = exec_filename->len; while ((--exec_fname_length >= 0) && !IS_SLASH(exec_fname[exec_fname_length])); - if ((exec_fname && exec_fname[0] == '[') - || exec_fname_length<=0) { - /* [no active file] or no path */ + if (exec_fname_length<=0) { + /* no path */ pathbuf = estrdup(path); } else { + size_t path_length = strlen(path); + pathbuf = (char *) emalloc(exec_fname_length + path_length +1 +1); memcpy(pathbuf, path, path_length); pathbuf[path_length] = DEFAULT_DIR_SEPARATOR;