Skip to content

Commit 9574627

Browse files
committed
Clean up recursive mkdir logic
Rewrite this as a loop with a single VCWD_MKDIR call and a subsequent advance to the next directory.
1 parent be2df43 commit 9574627

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

main/streams/plain_wrapper.c

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,8 +1369,11 @@ static int php_plain_files_mkdir(php_stream_wrapper *wrapper, const char *dir, i
13691369
return 0;
13701370
}
13711371

1372+
if (php_check_open_basedir(buf)) {
1373+
return 0;
1374+
}
1375+
13721376
/* we look for directory separator from the end of string, thus hopefully reducing our work load */
1373-
int ret;
13741377
char *p;
13751378
zend_stat_t sb;
13761379
size_t dir_len = strlen(dir), offset = 0;
@@ -1406,32 +1409,32 @@ static int php_plain_files_mkdir(php_stream_wrapper *wrapper, const char *dir, i
14061409
}
14071410
}
14081411

1409-
if (p == buf) {
1410-
ret = php_mkdir(dir, mode);
1411-
} else if (!(ret = php_mkdir(buf, mode))) {
1412-
if (!p) {
1413-
p = buf;
1412+
if (!p) {
1413+
p = buf;
1414+
}
1415+
while (true) {
1416+
int ret = VCWD_MKDIR(buf, (mode_t) mode);
1417+
if (ret < 0) {
1418+
if (options & REPORT_ERRORS) {
1419+
php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
1420+
}
1421+
return 0;
14141422
}
1415-
/* create any needed directories if the creation of the 1st directory worked */
1423+
1424+
bool replaced_slash = false;
14161425
while (++p != e) {
14171426
if (*p == '\0') {
1427+
replaced_slash = true;
14181428
*p = DEFAULT_SLASH;
1419-
if ((*(p+1) != '\0') &&
1420-
(ret = VCWD_MKDIR(buf, (mode_t)mode)) < 0) {
1421-
if (options & REPORT_ERRORS) {
1422-
php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
1423-
}
1429+
if (*(p+1) != '\0') {
14241430
break;
14251431
}
14261432
}
14271433
}
1428-
}
1429-
if (ret < 0) {
1430-
/* Failure */
1431-
return 0;
1432-
} else {
1433-
/* Success */
1434-
return 1;
1434+
if (p == e || !replaced_slash) {
1435+
/* No more directories to create */
1436+
return 1;
1437+
}
14351438
}
14361439
}
14371440

0 commit comments

Comments
 (0)