]> granicus.if.org Git - php/commitdiff
Avoid repeatable strlen() calls
authorDmitry Stogov <dmitry@zend.com>
Thu, 5 Mar 2015 15:18:39 +0000 (18:18 +0300)
committerDmitry Stogov <dmitry@zend.com>
Thu, 5 Mar 2015 15:18:39 +0000 (18:18 +0300)
Zend/zend_execute.h
Zend/zend_execute_API.c
ext/opcache/ZendAccelerator.c
main/fopen_wrappers.c
main/streams/plain_wrapper.c

index 4298f6bdd68969440d971b7b97954c4737bf752d..d33735291a267f5f50cea745bcab9dcc3aa2da04 100644 (file)
@@ -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);
 
index f52d9f33ce4d2e57086d54631cb243c5bae0eb36..3c1bd87bac112ad54e7f1cfaa2eeb498ba52c2c0 100644 (file)
@@ -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);
index a1e7d9c42d34863ba808671b300760fe562c80c3..8b56c86788f7affa9de9996964c56dea1426d817 100644 (file)
@@ -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++;
index c51648129850578dec553947d091278260494d6c..396b0c51a09ac7fd55163553cb28eaad04fc8cf2 100644 (file)
@@ -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;
index 83bd059c348291d547681c99e1e718e43e54a2e5..6af8ffa964fe4e6e9ead7af25315e4b07aa2390a 100644 (file)
@@ -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;