Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Zend: Refactor VCWD_CHDIR_FILE() to take path length
  • Loading branch information
Girgias committed Aug 23, 2024
commit 439f904b49d77510f0508b277d4b7d3ec0eeb8ac
35 changes: 17 additions & 18 deletions Zend/zend_virtual_cwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1206,39 +1206,38 @@ CWD_API zend_result virtual_chdir(const char *path) /* {{{ */
}
/* }}} */


/* returns 0 for ok, 1 for empty string, -1 on error */
CWD_API int virtual_chdir_file(const char *path, int (*p_chdir)(const char *path)) /* {{{ */
CWD_API zend_result virtual_chdir_file(const char *path, size_t path_len, int (*p_chdir)(const char *path)) /* {{{ */
{
size_t length = strlen(path);
char *temp;
int retval;
ALLOCA_FLAG(use_heap)

if (length == 0) {
return 1; /* Can't cd to empty string */
}
while(--length < SIZE_MAX && !IS_SLASH(path[length])) {
ZEND_ASSERT(path_len != 0 && "path must not be empty");
while(--path_len < SIZE_MAX && !IS_SLASH(path[path_len])) {
}

if (length == SIZE_MAX) {
if (path_len == SIZE_MAX) {
/* No directory only file name */
errno = ENOENT;
return -1;
return FAILURE;
}

if (length == COPY_WHEN_ABSOLUTE(path) && IS_ABSOLUTE_PATH(path, length+1)) { /* Also use trailing slash if this is absolute */
length++;
if (path_len == COPY_WHEN_ABSOLUTE(path) && IS_ABSOLUTE_PATH(path, path_len+1)) { /* Also use trailing slash if this is absolute */
path_len++;
}
temp = (char *) do_alloca(length+1, use_heap);
memcpy(temp, path, length);
temp[length] = 0;
temp = (char *) do_alloca(path_len+1, use_heap);
memcpy(temp, path, path_len);
temp[path_len] = 0;
#if VIRTUAL_CWD_DEBUG
fprintf (stderr, "Changing directory to %s\n", temp);
#endif
retval = p_chdir(temp);
int retval = p_chdir(temp);
free_alloca(temp, use_heap);
return retval;

if (retval == 0) {
return SUCCESS;
} else {
return FAILURE;
}
}
/* }}} */

Expand Down
6 changes: 3 additions & 3 deletions Zend/zend_virtual_cwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ CWD_API void virtual_cwd_deactivate(void);
CWD_API char *virtual_getcwd_ex(size_t *length);
CWD_API char *virtual_getcwd(char *buf, size_t size);
CWD_API zend_result virtual_chdir(const char *path);
CWD_API int virtual_chdir_file(const char *path, int (*p_chdir)(const char *path));
CWD_API zend_result virtual_chdir_file(const char *path, size_t path_len, int (*p_chdir)(const char *path));
CWD_API int virtual_filepath(const char *path, char **filepath);
CWD_API int virtual_filepath_ex(const char *path, char **filepath, verify_path_func verify_path);
CWD_API char *virtual_realpath(const char *path, char *real_path);
Expand Down Expand Up @@ -272,7 +272,7 @@ extern void virtual_cwd_main_cwd_init(uint8_t);
#define VCWD_OPEN_MODE(path, flags, mode) virtual_open(path, flags, mode)
#define VCWD_CREAT(path, mode) virtual_creat(path, mode)
#define VCWD_CHDIR(path) virtual_chdir(path)
#define VCWD_CHDIR_FILE(path) virtual_chdir_file(path, (int (*)(const char *)) virtual_chdir)
#define VCWD_CHDIR_FILE(path, path_len) virtual_chdir_file(path, path_len, (int (*)(const char *)) virtual_chdir)
#define VCWD_GETWD(buf)
#define VCWD_REALPATH(path, real_path) virtual_realpath(path, real_path)
#define VCWD_RENAME(oldname, newname) virtual_rename(oldname, newname)
Expand Down Expand Up @@ -326,7 +326,7 @@ extern void virtual_cwd_main_cwd_init(uint8_t);
#define VCWD_CHMOD(path, mode) chmod(path, mode)
#endif

#define VCWD_CHDIR_FILE(path) virtual_chdir_file(path, chdir)
#define VCWD_CHDIR_FILE(path, path_len) virtual_chdir_file(path, path_len, chdir)
#define VCWD_GETWD(buf) getwd(buf)
#define VCWD_STAT(path, buff) php_sys_stat(path, buff)
#define VCWD_LSTAT(path, buff) lstat(path, buff)
Expand Down
4 changes: 2 additions & 2 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2536,7 +2536,7 @@ PHPAPI bool php_execute_script_ex(zend_file_handle *primary_file, zval *retval)
#else
php_ignore_value(VCWD_GETCWD(old_cwd, OLD_CWD_SIZE-1));
#endif
VCWD_CHDIR_FILE(ZSTR_VAL(primary_file->filename));
VCWD_CHDIR_FILE(ZSTR_VAL(primary_file->filename), ZSTR_LEN(primary_file->filename));
}

/* Only lookup the real file path and add it to the included_files list if already opened
Expand Down Expand Up @@ -2639,7 +2639,7 @@ PHPAPI int php_execute_simple_script(zend_file_handle *primary_file, zval *ret)

if (primary_file->filename && !(SG(options) & SAPI_OPTION_NO_CHDIR)) {
php_ignore_value(VCWD_GETCWD(old_cwd, OLD_CWD_SIZE-1));
VCWD_CHDIR_FILE(ZSTR_VAL(primary_file->filename));
VCWD_CHDIR_FILE(ZSTR_VAL(primary_file->filename), ZSTR_LEN(primary_file->filename));
}
zend_execute_scripts(ZEND_REQUIRE, ret, 1, primary_file);
} zend_end_try();
Expand Down
2 changes: 1 addition & 1 deletion sapi/phpdbg/phpdbg_prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ PHPDBG_COMMAND(exec) /* {{{ */
PHPDBG_G(exec) = res;
PHPDBG_G(exec_len) = res_len;

VCWD_CHDIR_FILE(res);
VCWD_CHDIR_FILE(res, res_len);

*SG(request_info).argv = estrndup(PHPDBG_G(exec), PHPDBG_G(exec_len));
php_build_argv(NULL, &PG(http_globals)[TRACK_VARS_SERVER]);
Expand Down