Skip to content

Commit fdb3851

Browse files
committed
Zend: Refactor virtual_file_ex() to take path length
1 parent b7ec53f commit fdb3851

7 files changed

+54
-30
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_file_ex() function now has an extra parameter for the path
109+
length.
110+
108111
* The virtual_chdir_file() function and corresponding VCWD_CHDIR_FILE() macro
109112
now have an extra parameter for the path length.
110113

Zend/zend_virtual_cwd.c

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,9 +1006,8 @@ static size_t tsrm_realpath_r(char *path, size_t start, size_t len, int *ll, tim
10061006

10071007
/* Resolve path relatively to state and put the real path into state */
10081008
/* returns 0 for ok, 1 for error, -1 if (path_length >= MAXPATHLEN-1) */
1009-
CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func verify_path, int use_realpath) /* {{{ */
1009+
CWD_API int virtual_file_ex(cwd_state *state, const char *path, size_t path_length, verify_path_func verify_path, int use_realpath) /* {{{ */
10101010
{
1011-
size_t path_length = strlen(path);
10121011
char resolved_path[MAXPATHLEN];
10131012
size_t start = 1;
10141013
int ll = 0;
@@ -1204,7 +1203,8 @@ CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func
12041203

12051204
CWD_API zend_result virtual_chdir(const char *path) /* {{{ */
12061205
{
1207-
return virtual_file_ex(&CWDG(cwd), path, php_is_dir_ok, CWD_REALPATH) ? FAILURE : SUCCESS;
1206+
size_t path_length = strlen(path);
1207+
return virtual_file_ex(&CWDG(cwd), path, path_length, php_is_dir_ok, CWD_REALPATH) ? FAILURE : SUCCESS;
12081208
}
12091209
/* }}} */
12101210

@@ -1248,24 +1248,26 @@ CWD_API char *virtual_realpath(const char *path, char *real_path) /* {{{ */
12481248
cwd_state new_state;
12491249
char *retval;
12501250
char cwd[MAXPATHLEN];
1251+
size_t path_length = strlen(path);
12511252

12521253
/* realpath("") returns CWD */
1253-
if (!*path) {
1254+
if (path_length == 0) {
12541255
new_state.cwd = (char*)emalloc(1);
12551256
new_state.cwd[0] = '\0';
12561257
new_state.cwd_length = 0;
12571258
if (VCWD_GETCWD(cwd, MAXPATHLEN)) {
12581259
path = cwd;
1260+
path_length = strlen(cwd);
12591261
}
1260-
} else if (!IS_ABSOLUTE_PATH(path, strlen(path))) {
1262+
} else if (!IS_ABSOLUTE_PATH(path, path_length)) {
12611263
CWD_STATE_COPY(&new_state, &CWDG(cwd));
12621264
} else {
12631265
new_state.cwd = (char*)emalloc(1);
12641266
new_state.cwd[0] = '\0';
12651267
new_state.cwd_length = 0;
12661268
}
12671269

1268-
if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH)==0) {
1270+
if (virtual_file_ex(&new_state, path, path_length, NULL, CWD_REALPATH)==0) {
12691271
size_t len = new_state.cwd_length>MAXPATHLEN-1?MAXPATHLEN-1:new_state.cwd_length;
12701272

12711273
memcpy(real_path, new_state.cwd, len);
@@ -1284,10 +1286,11 @@ CWD_API char *virtual_realpath(const char *path, char *real_path) /* {{{ */
12841286
CWD_API int virtual_filepath_ex(const char *path, char **filepath, verify_path_func verify_path) /* {{{ */
12851287
{
12861288
cwd_state new_state;
1289+
size_t path_length = strlen(path);
12871290
int retval;
12881291

12891292
CWD_STATE_COPY(&new_state, &CWDG(cwd));
1290-
retval = virtual_file_ex(&new_state, path, verify_path, CWD_FILEPATH);
1293+
retval = virtual_file_ex(&new_state, path, path_length, verify_path, CWD_FILEPATH);
12911294

12921295
*filepath = new_state.cwd;
12931296

@@ -1306,14 +1309,15 @@ CWD_API int virtual_filepath(const char *path, char **filepath) /* {{{ */
13061309
CWD_API FILE *virtual_fopen(const char *path, const char *mode) /* {{{ */
13071310
{
13081311
cwd_state new_state;
1312+
size_t path_length = strlen(path);
13091313
FILE *f;
13101314

13111315
if (path[0] == '\0') { /* Fail to open empty path */
13121316
return NULL;
13131317
}
13141318

13151319
CWD_STATE_COPY(&new_state, &CWDG(cwd));
1316-
if (virtual_file_ex(&new_state, path, NULL, CWD_EXPAND)) {
1320+
if (virtual_file_ex(&new_state, path, path_length, NULL, CWD_EXPAND)) {
13171321
CWD_STATE_FREE_ERR(&new_state);
13181322
return NULL;
13191323
}
@@ -1333,10 +1337,11 @@ CWD_API FILE *virtual_fopen(const char *path, const char *mode) /* {{{ */
13331337
CWD_API int virtual_access(const char *pathname, int mode) /* {{{ */
13341338
{
13351339
cwd_state new_state;
1340+
size_t path_length = strlen(pathname);
13361341
int ret;
13371342

13381343
CWD_STATE_COPY(&new_state, &CWDG(cwd));
1339-
if (virtual_file_ex(&new_state, pathname, NULL, CWD_REALPATH)) {
1344+
if (virtual_file_ex(&new_state, pathname, path_length, NULL, CWD_REALPATH)) {
13401345
CWD_STATE_FREE_ERR(&new_state);
13411346
return -1;
13421347
}
@@ -1357,10 +1362,11 @@ CWD_API int virtual_access(const char *pathname, int mode) /* {{{ */
13571362
CWD_API int virtual_utime(const char *filename, struct utimbuf *buf) /* {{{ */
13581363
{
13591364
cwd_state new_state;
1365+
size_t filename_length = strlen(filename);
13601366
int ret;
13611367

13621368
CWD_STATE_COPY(&new_state, &CWDG(cwd));
1363-
if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH)) {
1369+
if (virtual_file_ex(&new_state, filename, filename_length, NULL, CWD_REALPATH)) {
13641370
CWD_STATE_FREE_ERR(&new_state);
13651371
return -1;
13661372
}
@@ -1380,10 +1386,11 @@ CWD_API int virtual_utime(const char *filename, struct utimbuf *buf) /* {{{ */
13801386
CWD_API int virtual_chmod(const char *filename, mode_t mode) /* {{{ */
13811387
{
13821388
cwd_state new_state;
1389+
size_t filename_length = strlen(filename);
13831390
int ret;
13841391

13851392
CWD_STATE_COPY(&new_state, &CWDG(cwd));
1386-
if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH)) {
1393+
if (virtual_file_ex(&new_state, filename, filename_length, NULL, CWD_REALPATH)) {
13871394
CWD_STATE_FREE_ERR(&new_state);
13881395
return -1;
13891396
}
@@ -1415,10 +1422,11 @@ CWD_API int virtual_chmod(const char *filename, mode_t mode) /* {{{ */
14151422
CWD_API int virtual_chown(const char *filename, uid_t owner, gid_t group, int link) /* {{{ */
14161423
{
14171424
cwd_state new_state;
1425+
size_t filename_length = strlen(filename);
14181426
int ret;
14191427

14201428
CWD_STATE_COPY(&new_state, &CWDG(cwd));
1421-
if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH)) {
1429+
if (virtual_file_ex(&new_state, filename, filename_length, NULL, CWD_REALPATH)) {
14221430
CWD_STATE_FREE_ERR(&new_state);
14231431
return -1;
14241432
}
@@ -1442,10 +1450,11 @@ CWD_API int virtual_chown(const char *filename, uid_t owner, gid_t group, int li
14421450
CWD_API int virtual_open(const char *path, int flags, ...) /* {{{ */
14431451
{
14441452
cwd_state new_state;
1453+
size_t path_length = strlen(path);
14451454
int f;
14461455

14471456
CWD_STATE_COPY(&new_state, &CWDG(cwd));
1448-
if (virtual_file_ex(&new_state, path, NULL, CWD_FILEPATH)) {
1457+
if (virtual_file_ex(&new_state, path, path_length, NULL, CWD_FILEPATH)) {
14491458
CWD_STATE_FREE_ERR(&new_state);
14501459
return -1;
14511460
}
@@ -1478,10 +1487,11 @@ CWD_API int virtual_open(const char *path, int flags, ...) /* {{{ */
14781487
CWD_API int virtual_creat(const char *path, mode_t mode) /* {{{ */
14791488
{
14801489
cwd_state new_state;
1490+
size_t path_length = strlen(path);
14811491
int f;
14821492

14831493
CWD_STATE_COPY(&new_state, &CWDG(cwd));
1484-
if (virtual_file_ex(&new_state, path, NULL, CWD_FILEPATH)) {
1494+
if (virtual_file_ex(&new_state, path, path_length, NULL, CWD_FILEPATH)) {
14851495
CWD_STATE_FREE_ERR(&new_state);
14861496
return -1;
14871497
}
@@ -1497,17 +1507,19 @@ CWD_API int virtual_rename(const char *oldname, const char *newname) /* {{{ */
14971507
{
14981508
cwd_state old_state;
14991509
cwd_state new_state;
1510+
size_t old_name_length = strlen(oldname);
1511+
size_t new_name_length = strlen(newname);
15001512
int retval;
15011513

15021514
CWD_STATE_COPY(&old_state, &CWDG(cwd));
1503-
if (virtual_file_ex(&old_state, oldname, NULL, CWD_EXPAND)) {
1515+
if (virtual_file_ex(&old_state, oldname, old_name_length, NULL, CWD_EXPAND)) {
15041516
CWD_STATE_FREE_ERR(&old_state);
15051517
return -1;
15061518
}
15071519
oldname = old_state.cwd;
15081520

15091521
CWD_STATE_COPY(&new_state, &CWDG(cwd));
1510-
if (virtual_file_ex(&new_state, newname, NULL, CWD_EXPAND)) {
1522+
if (virtual_file_ex(&new_state, newname, new_name_length, NULL, CWD_EXPAND)) {
15111523
CWD_STATE_FREE_ERR(&old_state);
15121524
CWD_STATE_FREE_ERR(&new_state);
15131525
return -1;
@@ -1534,9 +1546,10 @@ CWD_API int virtual_stat(const char *path, zend_stat_t *buf) /* {{{ */
15341546
{
15351547
cwd_state new_state;
15361548
int retval;
1549+
size_t path_length = strlen(path);
15371550

15381551
CWD_STATE_COPY(&new_state, &CWDG(cwd));
1539-
if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH)) {
1552+
if (virtual_file_ex(&new_state, path, path_length, NULL, CWD_REALPATH)) {
15401553
CWD_STATE_FREE_ERR(&new_state);
15411554
return -1;
15421555
}
@@ -1552,9 +1565,10 @@ CWD_API int virtual_lstat(const char *path, zend_stat_t *buf) /* {{{ */
15521565
{
15531566
cwd_state new_state;
15541567
int retval;
1568+
size_t path_length = strlen(path);
15551569

15561570
CWD_STATE_COPY(&new_state, &CWDG(cwd));
1557-
if (virtual_file_ex(&new_state, path, NULL, CWD_EXPAND)) {
1571+
if (virtual_file_ex(&new_state, path, path_length, NULL, CWD_EXPAND)) {
15581572
CWD_STATE_FREE_ERR(&new_state);
15591573
return -1;
15601574
}
@@ -1570,9 +1584,10 @@ CWD_API int virtual_unlink(const char *path) /* {{{ */
15701584
{
15711585
cwd_state new_state;
15721586
int retval;
1587+
size_t path_length = strlen(path);
15731588

15741589
CWD_STATE_COPY(&new_state, &CWDG(cwd));
1575-
if (virtual_file_ex(&new_state, path, NULL, CWD_EXPAND)) {
1590+
if (virtual_file_ex(&new_state, path, path_length, NULL, CWD_EXPAND)) {
15761591
CWD_STATE_FREE_ERR(&new_state);
15771592
return -1;
15781593
}
@@ -1592,9 +1607,10 @@ CWD_API int virtual_mkdir(const char *pathname, mode_t mode) /* {{{ */
15921607
{
15931608
cwd_state new_state;
15941609
int retval;
1610+
size_t path_length = strlen(pathname);
15951611

15961612
CWD_STATE_COPY(&new_state, &CWDG(cwd));
1597-
if (virtual_file_ex(&new_state, pathname, NULL, CWD_FILEPATH)) {
1613+
if (virtual_file_ex(&new_state, pathname, path_length, NULL, CWD_FILEPATH)) {
15981614
CWD_STATE_FREE_ERR(&new_state);
15991615
return -1;
16001616
}
@@ -1613,9 +1629,10 @@ CWD_API int virtual_rmdir(const char *pathname) /* {{{ */
16131629
{
16141630
cwd_state new_state;
16151631
int retval;
1632+
size_t path_length = strlen(pathname);
16161633

16171634
CWD_STATE_COPY(&new_state, &CWDG(cwd));
1618-
if (virtual_file_ex(&new_state, pathname, NULL, CWD_EXPAND)) {
1635+
if (virtual_file_ex(&new_state, pathname, path_length, NULL, CWD_EXPAND)) {
16191636
CWD_STATE_FREE_ERR(&new_state);
16201637
return -1;
16211638
}
@@ -1638,9 +1655,10 @@ CWD_API DIR *virtual_opendir(const char *pathname) /* {{{ */
16381655
{
16391656
cwd_state new_state;
16401657
DIR *retval;
1658+
size_t path_length = strlen(pathname);
16411659

16421660
CWD_STATE_COPY(&new_state, &CWDG(cwd));
1643-
if (virtual_file_ex(&new_state, pathname, NULL, CWD_REALPATH)) {
1661+
if (virtual_file_ex(&new_state, pathname, path_length, NULL, CWD_REALPATH)) {
16441662
CWD_STATE_FREE_ERR(&new_state);
16451663
return NULL;
16461664
}
@@ -1719,16 +1737,18 @@ CWD_API char *tsrm_realpath(const char *path, char *real_path) /* {{{ */
17191737
{
17201738
cwd_state new_state;
17211739
char cwd[MAXPATHLEN];
1740+
size_t path_length = strlen(path);
17221741

17231742
/* realpath("") returns CWD */
1724-
if (!*path) {
1743+
if (path_length == 0) {
17251744
new_state.cwd = (char*)emalloc(1);
17261745
new_state.cwd[0] = '\0';
17271746
new_state.cwd_length = 0;
17281747
if (VCWD_GETCWD(cwd, MAXPATHLEN)) {
17291748
path = cwd;
1749+
path_length = strlen(cwd);
17301750
}
1731-
} else if (!IS_ABSOLUTE_PATH(path, strlen(path)) &&
1751+
} else if (!IS_ABSOLUTE_PATH(path, path_length) &&
17321752
VCWD_GETCWD(cwd, MAXPATHLEN)) {
17331753
new_state.cwd = estrdup(cwd);
17341754
new_state.cwd_length = strlen(cwd);
@@ -1738,7 +1758,7 @@ CWD_API char *tsrm_realpath(const char *path, char *real_path) /* {{{ */
17381758
new_state.cwd_length = 0;
17391759
}
17401760

1741-
if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH)) {
1761+
if (virtual_file_ex(&new_state, path, path_length, NULL, CWD_REALPATH)) {
17421762
efree(new_state.cwd);
17431763
return NULL;
17441764
}

Zend/zend_virtual_cwd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ CWD_API int virtual_chown(const char *filename, uid_t owner, gid_t group, int li
206206
#define CWD_FILEPATH 1 /* resolve symlinks if file is exist otherwise expand */
207207
#define CWD_REALPATH 2 /* call realpath(), resolve symlinks. File must exist */
208208

209-
CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func verify_path, int use_realpath);
209+
CWD_API int virtual_file_ex(cwd_state *state, const char *path, size_t path_length, verify_path_func verify_path, int use_realpath);
210210

211211
CWD_API char *tsrm_realpath(const char *path, char *real_path);
212212

ext/phar/phar_object.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4130,7 +4130,7 @@ static int phar_extract_file(bool overwrite, phar_entry_info *entry, char *dest,
41304130
new_state.cwd[0] = DEFAULT_SLASH;
41314131
new_state.cwd[1] = '\0';
41324132
new_state.cwd_length = 1;
4133-
if (virtual_file_ex(&new_state, entry->filename, NULL, CWD_EXPAND) != 0 ||
4133+
if (virtual_file_ex(&new_state, entry->filename, entry->filename_len, NULL, CWD_EXPAND) != 0 ||
41344134
new_state.cwd_length <= 1) {
41354135
if (EINVAL == errno && entry->filename_len > 50) {
41364136
char *tmp = estrndup(entry->filename, 50);

ext/zip/php_zip.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ static int php_zip_extract_file(struct zip * za, char *dest, const char *file, s
152152
/* Clean/normlize the path and then transform any path (absolute or relative)
153153
to a path relative to cwd (../../mydir/foo.txt > mydir/foo.txt)
154154
*/
155-
virtual_file_ex(&new_state, file, NULL, CWD_EXPAND);
155+
virtual_file_ex(&new_state, file, file_len, NULL, CWD_EXPAND);
156156
path_cleaned = php_zip_make_relative_path(new_state.cwd, new_state.cwd_length);
157157
if(!path_cleaned) {
158158
CWD_STATE_FREE(new_state.cwd);

main/fopen_wrappers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ PHPAPI char *expand_filepath_with_mode(const char *filepath, char *real_path, co
834834
new_state.cwd = estrdup(cwd);
835835
new_state.cwd_length = strlen(cwd);
836836

837-
if (virtual_file_ex(&new_state, filepath, NULL, realpath_mode)) {
837+
if (virtual_file_ex(&new_state, filepath, path_len, NULL, realpath_mode)) {
838838
efree(new_state.cwd);
839839
return NULL;
840840
}

main/php_open_temporary_file.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,9 @@ static int php_do_open_temporary_file(const char *path, const char *pfx, zend_st
131131

132132
new_state.cwd = estrdup(cwd);
133133
new_state.cwd_length = strlen(cwd);
134+
size_t path_length = strlen(path);
134135

135-
if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH)) {
136+
if (virtual_file_ex(&new_state, path, path_length, NULL, CWD_REALPATH)) {
136137
efree(new_state.cwd);
137138
return -1;
138139
}

0 commit comments

Comments
 (0)