Skip to content

Commit 4a5b41f

Browse files
committed
Zend: Refactor VCWD_CHDIR_FILE() to take path length
1 parent 35fbb00 commit 4a5b41f

File tree

5 files changed

+26
-24
lines changed

5 files changed

+26
-24
lines changed

UPGRADING.INTERNALS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ PHP 8.4 INTERNALS UPGRADE NOTES
105105
* Removed ZEND_DIM_ALTERNATIVE_SYNTAX constant. This syntax no longer has a
106106
specialized error message.
107107

108+
* The virtual_chdir_file() function and corresponding VCWD_CHDIR_FILE() macro
109+
now have an extra parameter for the path length.
110+
108111
========================
109112
2. Build system changes
110113
========================

Zend/zend_virtual_cwd.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,39 +1208,38 @@ CWD_API zend_result virtual_chdir(const char *path) /* {{{ */
12081208
}
12091209
/* }}} */
12101210

1211-
1212-
/* returns 0 for ok, 1 for empty string, -1 on error */
1213-
CWD_API int virtual_chdir_file(const char *path, int (*p_chdir)(const char *path)) /* {{{ */
1211+
CWD_API zend_result virtual_chdir_file(const char *path, size_t path_len, int (*p_chdir)(const char *path)) /* {{{ */
12141212
{
1215-
size_t length = strlen(path);
12161213
char *temp;
1217-
int retval;
12181214
ALLOCA_FLAG(use_heap)
12191215

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

1226-
if (length == SIZE_MAX) {
1220+
if (path_len == SIZE_MAX) {
12271221
/* No directory only file name */
12281222
errno = ENOENT;
1229-
return -1;
1223+
return FAILURE;
12301224
}
12311225

1232-
if (length == COPY_WHEN_ABSOLUTE(path) && IS_ABSOLUTE_PATH(path, length+1)) { /* Also use trailing slash if this is absolute */
1233-
length++;
1226+
if (path_len == COPY_WHEN_ABSOLUTE(path) && IS_ABSOLUTE_PATH(path, path_len+1)) { /* Also use trailing slash if this is absolute */
1227+
path_len++;
12341228
}
1235-
temp = (char *) do_alloca(length+1, use_heap);
1236-
memcpy(temp, path, length);
1237-
temp[length] = 0;
1229+
temp = (char *) do_alloca(path_len+1, use_heap);
1230+
memcpy(temp, path, path_len);
1231+
temp[path_len] = 0;
12381232
#if VIRTUAL_CWD_DEBUG
12391233
fprintf (stderr, "Changing directory to %s\n", temp);
12401234
#endif
1241-
retval = p_chdir(temp);
1235+
int retval = p_chdir(temp);
12421236
free_alloca(temp, use_heap);
1243-
return retval;
1237+
1238+
if (retval == 0) {
1239+
return SUCCESS;
1240+
} else {
1241+
return FAILURE;
1242+
}
12441243
}
12451244
/* }}} */
12461245

Zend/zend_virtual_cwd.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ CWD_API int virtual_cwd_deactivate(void);
173173
CWD_API char *virtual_getcwd_ex(size_t *length);
174174
CWD_API char *virtual_getcwd(char *buf, size_t size);
175175
CWD_API zend_result virtual_chdir(const char *path);
176-
CWD_API int virtual_chdir_file(const char *path, int (*p_chdir)(const char *path));
176+
CWD_API zend_result virtual_chdir_file(const char *path, size_t path_len, int (*p_chdir)(const char *path));
177177
CWD_API int virtual_filepath(const char *path, char **filepath);
178178
CWD_API int virtual_filepath_ex(const char *path, char **filepath, verify_path_func verify_path);
179179
CWD_API char *virtual_realpath(const char *path, char *real_path);
@@ -272,7 +272,7 @@ extern void virtual_cwd_main_cwd_init(uint8_t);
272272
#define VCWD_OPEN_MODE(path, flags, mode) virtual_open(path, flags, mode)
273273
#define VCWD_CREAT(path, mode) virtual_creat(path, mode)
274274
#define VCWD_CHDIR(path) virtual_chdir(path)
275-
#define VCWD_CHDIR_FILE(path) virtual_chdir_file(path, (int (*)(const char *)) virtual_chdir)
275+
#define VCWD_CHDIR_FILE(path, path_len) virtual_chdir_file(path, path_len, (int (*)(const char *)) virtual_chdir)
276276
#define VCWD_GETWD(buf)
277277
#define VCWD_REALPATH(path, real_path) virtual_realpath(path, real_path)
278278
#define VCWD_RENAME(oldname, newname) virtual_rename(oldname, newname)
@@ -326,7 +326,7 @@ extern void virtual_cwd_main_cwd_init(uint8_t);
326326
#define VCWD_CHMOD(path, mode) chmod(path, mode)
327327
#endif
328328

329-
#define VCWD_CHDIR_FILE(path) virtual_chdir_file(path, chdir)
329+
#define VCWD_CHDIR_FILE(path, path_len) virtual_chdir_file(path, path_len, chdir)
330330
#define VCWD_GETWD(buf) getwd(buf)
331331
#define VCWD_STAT(path, buff) php_sys_stat(path, buff)
332332
#define VCWD_LSTAT(path, buff) lstat(path, buff)

main/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,7 +2536,7 @@ PHPAPI bool php_execute_script_ex(zend_file_handle *primary_file, zval *retval)
25362536
#else
25372537
php_ignore_value(VCWD_GETCWD(old_cwd, OLD_CWD_SIZE-1));
25382538
#endif
2539-
VCWD_CHDIR_FILE(ZSTR_VAL(primary_file->filename));
2539+
VCWD_CHDIR_FILE(ZSTR_VAL(primary_file->filename), ZSTR_LEN(primary_file->filename));
25402540
}
25412541

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

26402640
if (primary_file->filename && !(SG(options) & SAPI_OPTION_NO_CHDIR)) {
26412641
php_ignore_value(VCWD_GETCWD(old_cwd, OLD_CWD_SIZE-1));
2642-
VCWD_CHDIR_FILE(ZSTR_VAL(primary_file->filename));
2642+
VCWD_CHDIR_FILE(ZSTR_VAL(primary_file->filename), ZSTR_LEN(primary_file->filename));
26432643
}
26442644
zend_execute_scripts(ZEND_REQUIRE, ret, 1, primary_file);
26452645
} zend_end_try();

sapi/phpdbg/phpdbg_prompt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ PHPDBG_COMMAND(exec) /* {{{ */
427427
PHPDBG_G(exec) = res;
428428
PHPDBG_G(exec_len) = res_len;
429429

430-
VCWD_CHDIR_FILE(res);
430+
VCWD_CHDIR_FILE(res, res_len);
431431

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

0 commit comments

Comments
 (0)