Skip to content

Commit 6360082

Browse files
committed
Zend: Refactor virtual_rename() to take path lengths
1 parent fdb3851 commit 6360082

File tree

8 files changed

+62
-52
lines changed

8 files changed

+62
-52
lines changed

UPGRADING.INTERNALS

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ PHP 8.4 INTERNALS UPGRADE NOTES
1010

1111
5. SAPI changes
1212

13+
6. Windows changes
14+
1315
========================
1416
1. Internal API changes
1517
========================
@@ -111,6 +113,9 @@ PHP 8.4 INTERNALS UPGRADE NOTES
111113
* The virtual_chdir_file() function and corresponding VCWD_CHDIR_FILE() macro
112114
now have an extra parameter for the path length.
113115

116+
* The virtual_rename() function and corresponding VCWD_RENAME() macro
117+
now have extra parameters for the path lengths.
118+
114119
========================
115120
2. Build system changes
116121
========================
@@ -384,3 +389,10 @@ PHP 8.4 INTERNALS UPGRADE NOTES
384389
========================
385390
5. SAPI changes
386391
========================
392+
393+
========================
394+
6. Windows changes
395+
========================
396+
397+
* The php_win32_ioutil_rename() function now takes extra arguments for the
398+
lengths of the names.

Zend/zend_virtual_cwd.c

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,36 +1503,31 @@ CWD_API int virtual_creat(const char *path, mode_t mode) /* {{{ */
15031503
}
15041504
/* }}} */
15051505

1506-
CWD_API int virtual_rename(const char *oldname, const char *newname) /* {{{ */
1506+
CWD_API zend_result virtual_rename(const char *old_name, size_t old_name_len, const char *new_name, size_t new_name_len) /* {{{ */
15071507
{
15081508
cwd_state old_state;
15091509
cwd_state new_state;
1510-
size_t old_name_length = strlen(oldname);
1511-
size_t new_name_length = strlen(newname);
1512-
int retval;
15131510

15141511
CWD_STATE_COPY(&old_state, &CWDG(cwd));
1515-
if (virtual_file_ex(&old_state, oldname, old_name_length, NULL, CWD_EXPAND)) {
1512+
if (virtual_file_ex(&old_state, old_name, old_name_len, NULL, CWD_EXPAND)) {
15161513
CWD_STATE_FREE_ERR(&old_state);
1517-
return -1;
1514+
return FAILURE;
15181515
}
1519-
oldname = old_state.cwd;
15201516

15211517
CWD_STATE_COPY(&new_state, &CWDG(cwd));
1522-
if (virtual_file_ex(&new_state, newname, new_name_length, NULL, CWD_EXPAND)) {
1518+
if (virtual_file_ex(&new_state, new_name, new_name_len, NULL, CWD_EXPAND)) {
15231519
CWD_STATE_FREE_ERR(&old_state);
15241520
CWD_STATE_FREE_ERR(&new_state);
1525-
return -1;
1521+
return FAILURE;
15261522
}
1527-
newname = new_state.cwd;
15281523

1529-
/* rename on windows will fail if newname already exists.
1530-
MoveFileEx has to be used */
1524+
zend_result retval;
15311525
#ifdef ZEND_WIN32
1532-
/* MoveFileEx returns 0 on failure, other way 'round for this function */
1533-
retval = php_win32_ioutil_rename(oldname, newname);
1526+
/* rename on windows will fail if new_name already exists. MoveFileEx has to be used */
1527+
/* MoveFileEx returns 0 on failure, other way round for this function */
1528+
retval = php_win32_ioutil_rename(old_state.cwd, old_state.cwd_length, new_name, new_name_len);
15341529
#else
1535-
retval = rename(oldname, newname);
1530+
retval = virtual_rename_native(old_state.cwd, old_state.cwd_length, new_state.cwd, new_state.cwd_length);
15361531
#endif
15371532

15381533
CWD_STATE_FREE_ERR(&old_state);

Zend/zend_virtual_cwd.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ CWD_API char *virtual_realpath(const char *path, char *real_path);
180180
CWD_API FILE *virtual_fopen(const char *path, const char *mode);
181181
CWD_API int virtual_open(const char *path, int flags, ...);
182182
CWD_API int virtual_creat(const char *path, mode_t mode);
183-
CWD_API int virtual_rename(const char *oldname, const char *newname);
183+
CWD_API zend_result virtual_rename(const char *old_name, size_t old_name_len, const char *new_name, size_t new_name_len);
184184
CWD_API int virtual_stat(const char *path, zend_stat_t *buf);
185185
CWD_API int virtual_lstat(const char *path, zend_stat_t *buf);
186186
CWD_API int virtual_unlink(const char *path);
@@ -254,6 +254,12 @@ CWD_API zend_long realpath_cache_size(void);
254254
CWD_API zend_long realpath_cache_max_buckets(void);
255255
CWD_API realpath_cache_bucket** realpath_cache_get_buckets(void);
256256

257+
258+
static zend_always_inline zend_result virtual_rename_native(const char *old_name,
259+
ZEND_ATTRIBUTE_UNUSED size_t old_name_len, const char *new_name, ZEND_ATTRIBUTE_UNUSED size_t new_name_len) {
260+
return (rename(old_name, new_name) == 0) ? SUCCESS : FAILURE;
261+
}
262+
257263
#ifdef CWD_EXPORTS
258264
extern void virtual_cwd_main_cwd_init(uint8_t);
259265
#endif
@@ -275,7 +281,7 @@ extern void virtual_cwd_main_cwd_init(uint8_t);
275281
#define VCWD_CHDIR_FILE(path, path_len) virtual_chdir_file(path, path_len, (int (*)(const char *)) virtual_chdir)
276282
#define VCWD_GETWD(buf)
277283
#define VCWD_REALPATH(path, real_path) virtual_realpath(path, real_path)
278-
#define VCWD_RENAME(oldname, newname) virtual_rename(oldname, newname)
284+
#define VCWD_RENAME(old_name, old_name_length, new_name, new_name_length) virtual_rename(old_name, old_name_length, new_name, new_name_length)
279285
#define VCWD_STAT(path, buff) virtual_stat(path, buff)
280286
# define VCWD_LSTAT(path, buff) virtual_lstat(path, buff)
281287
#define VCWD_UNLINK(path) virtual_unlink(path)
@@ -304,7 +310,7 @@ extern void virtual_cwd_main_cwd_init(uint8_t);
304310
#define VCWD_FOPEN(path, mode) php_win32_ioutil_fopen(path, mode)
305311
#define VCWD_OPEN(path, flags) php_win32_ioutil_open(path, flags)
306312
#define VCWD_OPEN_MODE(path, flags, mode) php_win32_ioutil_open(path, flags, mode)
307-
# define VCWD_RENAME(oldname, newname) php_win32_ioutil_rename(oldname, newname)
313+
#define VCWD_RENAME(old_name, old_name_length, new_name, new_name_length) php_win32_ioutil_rename(old_name, old_name_length, new_name, new_name_length)
308314
#define VCWD_MKDIR(pathname, mode) php_win32_ioutil_mkdir(pathname, mode)
309315
#define VCWD_RMDIR(pathname) php_win32_ioutil_rmdir(pathname)
310316
#define VCWD_UNLINK(path) php_win32_ioutil_unlink(path)
@@ -316,8 +322,8 @@ extern void virtual_cwd_main_cwd_init(uint8_t);
316322
#define VCWD_FOPEN(path, mode) fopen(path, mode)
317323
#define VCWD_OPEN(path, flags) open(path, flags)
318324
#define VCWD_OPEN_MODE(path, flags, mode) open(path, flags, mode)
319-
# define VCWD_RENAME(oldname, newname) rename(oldname, newname)
320-
#define VCWD_MKDIR(pathname, mode) mkdir(pathname, mode)
325+
#define VCWD_RENAME(old_name, old_name_len, new_name, new_name_len) virtual_rename_native(old_name, old_name_len, new_name, new_name_len)
326+
#define VCWD_MKDIR(path, mode) mkdir(path, mode)
321327
#define VCWD_RMDIR(pathname) rmdir(pathname)
322328
#define VCWD_UNLINK(path) unlink(path)
323329
#define VCWD_CHDIR(path) chdir(path)

ext/soap/php_sdl.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,7 +2088,7 @@ static void sdl_serialize_soap_body(const sdlSoapBindingFunctionBodyPtr body, co
20882088
}
20892089
}
20902090

2091-
static void add_sdl_to_cache(const char *fn, const char *uri, time_t t, sdlPtr sdl)
2091+
static void add_sdl_to_cache(const char *fn, size_t fn_len, const char *uri, time_t t, sdlPtr sdl)
20922092
{
20932093
smart_str buf = {0};
20942094
smart_str *out = &buf;
@@ -2363,7 +2363,7 @@ static void add_sdl_to_cache(const char *fn, const char *uri, time_t t, sdlPtr s
23632363
/* Make sure that incomplete files (e.g. due to disk space issues, see bug #66150) are not utilised. */
23642364
if (valid_file) {
23652365
/* This is allowed to fail, this means that another process was raced to create the file. */
2366-
if (VCWD_RENAME(ZSTR_VAL(temp_file_path), fn) < 0) {
2366+
if (VCWD_RENAME(ZSTR_VAL(temp_file_path), ZSTR_LEN(temp_file_path), fn, fn_len) < 0) {
23672367
VCWD_UNLINK(ZSTR_VAL(temp_file_path));
23682368
}
23692369
}
@@ -3329,7 +3329,8 @@ sdlPtr get_sdl(zval *this_ptr, char *uri, zend_long cache_wsdl)
33293329

33303330
if ((cache_wsdl & WSDL_CACHE_DISK) && key) {
33313331
if (sdl) {
3332-
add_sdl_to_cache(key, uri, t, sdl);
3332+
// TODO Is it possible to know key_len before?
3333+
add_sdl_to_cache(key, strlen(key), uri, t, sdl);
33333334
}
33343335
efree(key);
33353336
}

ext/standard/basic_functions.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2379,7 +2379,7 @@ PHP_FUNCTION(move_uploaded_file)
23792379
RETURN_FALSE;
23802380
}
23812381

2382-
if (VCWD_RENAME(path, new_path) == 0) {
2382+
if (VCWD_RENAME(path, path_len, new_path, new_path_len) == 0) {
23832383
successful = 1;
23842384
#ifndef PHP_WIN32
23852385
oldmask = umask(077);

main/streams/plain_wrapper.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,30 +1274,34 @@ static int php_plain_files_rename(php_stream_wrapper *wrapper, const char *url_f
12741274
return 0;
12751275
}
12761276

1277+
size_t url_from_len = strlen(url_from);
1278+
size_t url_to_len = strlen(url_to);
12771279
#ifdef PHP_WIN32
1278-
if (!php_win32_check_trailing_space(url_from, strlen(url_from))) {
1280+
if (!php_win32_check_trailing_space(url_from, url_from_len)) {
12791281
php_win32_docref2_from_error(ERROR_INVALID_NAME, url_from, url_to);
12801282
return 0;
12811283
}
1282-
if (!php_win32_check_trailing_space(url_to, strlen(url_to))) {
1284+
if (!php_win32_check_trailing_space(url_to, url_to_len)) {
12831285
php_win32_docref2_from_error(ERROR_INVALID_NAME, url_from, url_to);
12841286
return 0;
12851287
}
12861288
#endif
12871289

12881290
if (strncasecmp(url_from, "file://", sizeof("file://") - 1) == 0) {
12891291
url_from += sizeof("file://") - 1;
1292+
url_from_len -= sizeof("file://");
12901293
}
12911294

12921295
if (strncasecmp(url_to, "file://", sizeof("file://") - 1) == 0) {
12931296
url_to += sizeof("file://") - 1;
1297+
url_to_len -= sizeof("file://");
12941298
}
12951299

12961300
if (php_check_open_basedir(url_from) || php_check_open_basedir(url_to)) {
12971301
return 0;
12981302
}
12991303

1300-
ret = VCWD_RENAME(url_from, url_to);
1304+
ret = VCWD_RENAME(url_from, url_from_len, url_to, url_to_len);
13011305

13021306
if (ret == -1) {
13031307
#ifndef PHP_WIN32

win32/ioutil.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -464,21 +464,18 @@ PW32IO int php_win32_ioutil_chdir_w(const wchar_t *path)
464464
return ret;
465465
}/*}}}*/
466466

467-
PW32IO int php_win32_ioutil_rename_w(const wchar_t *oldname, const wchar_t *newname)
467+
PW32IO zend_result php_win32_ioutil_rename_w(const wchar_t *oldname, const wchar_t *newname)
468468
{/*{{{*/
469-
int ret = 0;
470-
471-
PHP_WIN32_IOUTIL_CHECK_PATH_W(oldname, -1, 0)
472-
PHP_WIN32_IOUTIL_CHECK_PATH_W(newname, -1, 0)
473-
469+
PHP_WIN32_IOUTIL_CHECK_PATH_W(oldname, FAILURE, 0)
470+
PHP_WIN32_IOUTIL_CHECK_PATH_W(newname, FAILURE, 0)
474471

475472
if (!MoveFileExW(oldname, newname, MOVEFILE_REPLACE_EXISTING|MOVEFILE_COPY_ALLOWED)) {
476473
DWORD err = GetLastError();
477-
ret = -1;
478474
SET_ERRNO_FROM_WIN32_CODE(err);
475+
return FAILURE;
479476
}
480477

481-
return ret;
478+
return SUCCESS;
482479
}/*}}}*/
483480

484481
PW32IO wchar_t *php_win32_ioutil_getcwd_w(wchar_t *buf, size_t len)

win32/ioutil.h

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ PW32IO size_t php_win32_ioutil_dirname(char *buf, size_t len);
264264

265265
PW32IO int php_win32_ioutil_open_w(const wchar_t *path, int flags, ...);
266266
PW32IO int php_win32_ioutil_chdir_w(const wchar_t *path);
267-
PW32IO int php_win32_ioutil_rename_w(const wchar_t *oldname, const wchar_t *newname);
267+
PW32IO zend_result php_win32_ioutil_rename_w(const wchar_t *oldname, const wchar_t *newname);
268268
PW32IO wchar_t *php_win32_ioutil_getcwd_w(wchar_t *buf, size_t len);
269269
PW32IO int php_win32_ioutil_unlink_w(const wchar_t *path);
270270
PW32IO int php_win32_ioutil_access_w(const wchar_t *path, mode_t mode);
@@ -418,47 +418,42 @@ __forceinline static FILE *php_win32_ioutil_fopen(const char *patha, const char
418418
return ret;
419419
}/*}}}*/
420420

421-
__forceinline static int php_win32_ioutil_rename(const char *oldnamea, const char *newnamea)
421+
__forceinline static zend_result php_win32_ioutil_rename(const char *old_name_a, size_t old_name_a_len, const char *new_name_a, size_t new_name_a_len)
422422
{/*{{{*/
423423
wchar_t *oldnamew;
424424
wchar_t *newnamew;
425-
int ret;
426-
DWORD err = 0;
427425

428-
oldnamew = php_win32_ioutil_any_to_w(oldnamea);
426+
oldnamew = php_win32_ioutil_conv_any_to_w(old_name_a, old_name_a_len, PHP_WIN32_CP_IGNORE_LEN_P);
429427
if (!oldnamew) {
430428
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
431-
return -1;
429+
return FAILURE;
432430
}
433-
PHP_WIN32_IOUTIL_CHECK_PATH_W(oldnamew, -1, 1)
431+
PHP_WIN32_IOUTIL_CHECK_PATH_W(oldnamew, FAILURE, 1)
434432

435-
newnamew = php_win32_ioutil_any_to_w(newnamea);
433+
newnamew = php_win32_ioutil_conv_any_to_w(new_name_a, new_name_a_len, PHP_WIN32_CP_IGNORE_LEN_P);
436434
if (!newnamew) {
437435
free(oldnamew);
438436
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
439-
return -1;
437+
return FAILURE;
440438
} else {
441439
size_t newnamew_len = wcslen(newnamew);
442440
if (!PHP_WIN32_IOUTIL_PATH_IS_OK_W(newnamew, newnamew_len)) {
443441
free(oldnamew);
444442
free(newnamew);
445443
SET_ERRNO_FROM_WIN32_CODE(ERROR_ACCESS_DENIED);
446-
return -1;
444+
return FAILURE;
447445
}
448446
}
449447

450-
ret = php_win32_ioutil_rename_w(oldnamew, newnamew);
451-
if (0 > ret) {
452-
err = GetLastError();
448+
zend_result ret = php_win32_ioutil_rename_w(oldnamew, newnamew);
449+
if (ret == FAILURE) {
450+
DWORD err = GetLastError();
451+
SET_ERRNO_FROM_WIN32_CODE(err);
453452
}
454453

455454
free(oldnamew);
456455
free(newnamew);
457456

458-
if (0 > ret) {
459-
SET_ERRNO_FROM_WIN32_CODE(err);
460-
}
461-
462457
return ret;
463458
}/*}}}*/
464459

0 commit comments

Comments
 (0)