Skip to content

Commit 083d7f5

Browse files
committed
Reduce stack usage in php_resolve_path()
tsrm_realpath() internally always allocates a string. If the out parameter is provided it gets copied there and freed. What we were doing here was to first copy the path from the allocated string to a stack buffer, and then copy it from the stack buffer to a zend_string. We might as well save one copy and one buffer.
1 parent 3321440 commit 083d7f5

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

main/fopen_wrappers.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -458,12 +458,22 @@ PHPAPI int php_fopen_primary_script(zend_file_handle *file_handle)
458458
}
459459
/* }}} */
460460

461+
static zend_string *tsrm_realpath_str(const char *path) {
462+
char *realpath = tsrm_realpath(path, NULL);
463+
if (!realpath) {
464+
return NULL;
465+
}
466+
zend_string *realpath_str = zend_string_init(realpath, strlen(realpath), 0);
467+
efree(realpath);
468+
return realpath_str;
469+
}
470+
461471
/* {{{ php_resolve_path
462472
* Returns the realpath for given filename according to include path
463473
*/
464474
PHPAPI zend_string *php_resolve_path(const char *filename, size_t filename_length, const char *path)
465475
{
466-
char resolved_path[MAXPATHLEN];
476+
zend_string *resolved_path;
467477
char trypath[MAXPATHLEN];
468478
const char *ptr, *end, *p;
469479
const char *actual_path;
@@ -479,8 +489,8 @@ PHPAPI zend_string *php_resolve_path(const char *filename, size_t filename_lengt
479489
if ((*p == ':') && (p - filename > 1) && (p[1] == '/') && (p[2] == '/')) {
480490
wrapper = php_stream_locate_url_wrapper(filename, &actual_path, STREAM_OPEN_FOR_INCLUDE);
481491
if (wrapper == &php_plain_files_wrapper) {
482-
if (tsrm_realpath(actual_path, resolved_path)) {
483-
return zend_string_init(resolved_path, strlen(resolved_path), 0);
492+
if ((resolved_path = tsrm_realpath_str(actual_path))) {
493+
return resolved_path;
484494
}
485495
}
486496
return NULL;
@@ -499,11 +509,7 @@ PHPAPI zend_string *php_resolve_path(const char *filename, size_t filename_lengt
499509
#endif
500510
!path ||
501511
!*path) {
502-
if (tsrm_realpath(filename, resolved_path)) {
503-
return zend_string_init(resolved_path, strlen(resolved_path), 0);
504-
} else {
505-
return NULL;
506-
}
512+
return tsrm_realpath_str(filename);
507513
}
508514

509515
ptr = path;
@@ -559,8 +565,8 @@ PHPAPI zend_string *php_resolve_path(const char *filename, size_t filename_lengt
559565
continue;
560566
}
561567
}
562-
if (tsrm_realpath(actual_path, resolved_path)) {
563-
return zend_string_init(resolved_path, strlen(resolved_path), 0);
568+
if ((resolved_path = tsrm_realpath_str(actual_path))) {
569+
return resolved_path;
564570
}
565571
} /* end provided path */
566572

@@ -600,9 +606,7 @@ PHPAPI zend_string *php_resolve_path(const char *filename, size_t filename_lengt
600606
}
601607
}
602608

603-
if (tsrm_realpath(actual_path, resolved_path)) {
604-
return zend_string_init(resolved_path, strlen(resolved_path), 0);
605-
}
609+
return tsrm_realpath_str(actual_path);
606610
}
607611
}
608612

0 commit comments

Comments
 (0)