Skip to content

Commit 6d065a8

Browse files
committed
Avoid repeatable strlen() calls
1 parent bb91bf8 commit 6d065a8

File tree

5 files changed

+44
-29
lines changed

5 files changed

+44
-29
lines changed

Zend/zend_execute.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ static zend_always_inline void zend_vm_stack_free_call_frame(zend_execute_data *
221221
ZEND_API const char *get_active_class_name(const char **space);
222222
ZEND_API const char *get_active_function_name(void);
223223
ZEND_API const char *zend_get_executed_filename(void);
224+
ZEND_API zend_string *zend_get_executed_filename_ex(void);
224225
ZEND_API uint zend_get_executed_lineno(void);
225226
ZEND_API zend_bool zend_is_executing(void);
226227

Zend/zend_execute_API.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,21 @@ ZEND_API const char *zend_get_executed_filename(void) /* {{{ */
461461
}
462462
/* }}} */
463463

464+
ZEND_API zend_string *zend_get_executed_filename_ex(void) /* {{{ */
465+
{
466+
zend_execute_data *ex = EG(current_execute_data);
467+
468+
while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
469+
ex = ex->prev_execute_data;
470+
}
471+
if (ex) {
472+
return ex->func->op_array.filename;
473+
} else {
474+
return NULL;
475+
}
476+
}
477+
/* }}} */
478+
464479
ZEND_API uint zend_get_executed_lineno(void) /* {{{ */
465480
{
466481
zend_execute_data *ex = EG(current_execute_data);

ext/opcache/ZendAccelerator.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -927,8 +927,8 @@ char *accel_make_persistent_key_ex(zend_file_handle *file_handle, int path_lengt
927927
!is_stream_path(file_handle->filename)) {
928928
char *include_path = NULL;
929929
int include_path_len = 0;
930-
const char *parent_script = NULL;
931-
int parent_script_len = 0;
930+
zend_string *parent_script = NULL;
931+
size_t parent_script_len = 0;
932932
int cur_len = 0;
933933
int cwd_len;
934934
char *cwd;
@@ -990,11 +990,10 @@ char *accel_make_persistent_key_ex(zend_file_handle *file_handle, int path_lengt
990990
in include path too.
991991
*/
992992
if (EG(current_execute_data) &&
993-
(parent_script = zend_get_executed_filename()) != NULL &&
994-
parent_script[0] != '[') {
993+
(parent_script = zend_get_executed_filename_ex()) != NULL) {
995994

996-
parent_script_len = strlen(parent_script);
997-
while ((--parent_script_len > 0) && !IS_SLASH(parent_script[parent_script_len]));
995+
parent_script_len = parent_script->len;
996+
while ((--parent_script_len > 0) && !IS_SLASH(parent_script->val[parent_script_len]));
998997
}
999998

1000999
/* Calculate key length */
@@ -1022,7 +1021,7 @@ char *accel_make_persistent_key_ex(zend_file_handle *file_handle, int path_lengt
10221021
cur_len = cwd_len + 1 + path_length + 1;
10231022

10241023
if (parent_script_len) {
1025-
memcpy(ZCG(key) + cur_len, parent_script, parent_script_len);
1024+
memcpy(ZCG(key) + cur_len, parent_script->val, parent_script_len);
10261025
cur_len += parent_script_len;
10271026
ZCG(key)[cur_len] = ':';
10281027
cur_len++;

main/fopen_wrappers.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ PHPAPI zend_string *php_resolve_path(const char *filename, int filename_length,
493493
const char *ptr, *end, *p;
494494
const char *actual_path;
495495
php_stream_wrapper *wrapper;
496+
zend_string *exec_filename;
496497

497498
if (!filename || CHECK_NULL_PATH(filename, filename_length)) {
498499
return NULL;
@@ -580,13 +581,13 @@ PHPAPI zend_string *php_resolve_path(const char *filename, int filename_length,
580581

581582
/* check in calling scripts' current working directory as a fall back case
582583
*/
583-
if (zend_is_executing()) {
584-
const char *exec_fname = zend_get_executed_filename();
585-
int exec_fname_length = (int)strlen(exec_fname);
584+
if (zend_is_executing() &&
585+
(exec_filename = zend_get_executed_filename_ex()) != NULL) {
586+
const char *exec_fname = exec_filename->val;
587+
size_t exec_fname_length = exec_filename->len;
586588

587589
while ((--exec_fname_length >= 0) && !IS_SLASH(exec_fname[exec_fname_length]));
588-
if (exec_fname && exec_fname[0] != '[' &&
589-
exec_fname_length > 0 &&
590+
if (exec_fname_length > 0 &&
590591
exec_fname_length + 1 + filename_length + 1 < MAXPATHLEN) {
591592
memcpy(trypath, exec_fname, exec_fname_length + 1);
592593
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,
627628
PHPAPI FILE *php_fopen_with_path(const char *filename, const char *mode, const char *path, zend_string **opened_path)
628629
{
629630
char *pathbuf, *ptr, *end;
630-
const char *exec_fname;
631631
char trypath[MAXPATHLEN];
632632
FILE *fp;
633-
int path_length;
634633
int filename_length;
635-
int exec_fname_length;
634+
zend_string *exec_filename;
636635

637636
if (opened_path) {
638637
*opened_path = NULL;
@@ -660,16 +659,18 @@ PHPAPI FILE *php_fopen_with_path(const char *filename, const char *mode, const c
660659
/* append the calling scripts' current working directory
661660
* as a fall back case
662661
*/
663-
if (zend_is_executing()) {
664-
exec_fname = zend_get_executed_filename();
665-
exec_fname_length = (int)strlen(exec_fname);
666-
path_length = (int)strlen(path);
662+
if (zend_is_executing() &&
663+
(exec_filename = zend_get_executed_filename_ex()) != NULL) {
664+
const char *exec_fname = exec_filename->val;
665+
size_t exec_fname_length = exec_filename->len;
667666

668667
while ((--exec_fname_length >= 0) && !IS_SLASH(exec_fname[exec_fname_length]));
669668
if ((exec_fname && exec_fname[0] == '[') || exec_fname_length <= 0) {
670669
/* [no active file] or no path */
671670
pathbuf = estrdup(path);
672671
} else {
672+
size_t path_length = strlen(path);
673+
673674
pathbuf = (char *) emalloc(exec_fname_length + path_length + 1 + 1);
674675
memcpy(pathbuf, path, path_length);
675676
pathbuf[path_length] = DEFAULT_DIR_SEPARATOR;

main/streams/plain_wrapper.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,12 +1390,10 @@ PHPAPI php_stream *_php_stream_fopen_with_path(const char *filename, const char
13901390
/* code ripped off from fopen_wrappers.c */
13911391
char *pathbuf, *end;
13921392
const char *ptr;
1393-
const char *exec_fname;
13941393
char trypath[MAXPATHLEN];
13951394
php_stream *stream;
1396-
int path_length;
13971395
int filename_length;
1398-
int exec_fname_length;
1396+
zend_string *exec_filename;
13991397

14001398
if (opened_path) {
14011399
*opened_path = NULL;
@@ -1471,17 +1469,18 @@ PHPAPI php_stream *_php_stream_fopen_with_path(const char *filename, const char
14711469
/* append the calling scripts' current working directory
14721470
* as a fall back case
14731471
*/
1474-
if (zend_is_executing()) {
1475-
exec_fname = zend_get_executed_filename();
1476-
exec_fname_length = (int)strlen(exec_fname);
1477-
path_length = (int)strlen(path);
1472+
if (zend_is_executing() &&
1473+
(exec_filename = zend_get_executed_filename_ex()) != NULL) {
1474+
const char *exec_fname = exec_filename->val;
1475+
size_t exec_fname_length = exec_filename->len;
14781476

14791477
while ((--exec_fname_length >= 0) && !IS_SLASH(exec_fname[exec_fname_length]));
1480-
if ((exec_fname && exec_fname[0] == '[')
1481-
|| exec_fname_length<=0) {
1482-
/* [no active file] or no path */
1478+
if (exec_fname_length<=0) {
1479+
/* no path */
14831480
pathbuf = estrdup(path);
14841481
} else {
1482+
size_t path_length = strlen(path);
1483+
14851484
pathbuf = (char *) emalloc(exec_fname_length + path_length +1 +1);
14861485
memcpy(pathbuf, path, path_length);
14871486
pathbuf[path_length] = DEFAULT_DIR_SEPARATOR;

0 commit comments

Comments
 (0)