Skip to content

Commit 1bca728

Browse files
committed
Zend: Refactor virtual_unlink() to take path lengths
1 parent 9a19273 commit 1bca728

File tree

12 files changed

+60
-58
lines changed

12 files changed

+60
-58
lines changed

Zend/zend_virtual_cwd.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,22 +1575,21 @@ CWD_API int virtual_lstat(const char *path, zend_stat_t *buf) /* {{{ */
15751575
}
15761576
/* }}} */
15771577

1578-
CWD_API int virtual_unlink(const char *path) /* {{{ */
1578+
CWD_API zend_result virtual_unlink(const char *path, size_t path_length) /* {{{ */
15791579
{
15801580
cwd_state new_state;
1581-
int retval;
1582-
size_t path_length = strlen(path);
1581+
zend_result retval;
15831582

15841583
CWD_STATE_COPY(&new_state, &CWDG(cwd));
15851584
if (virtual_file_ex(&new_state, path, path_length, NULL, CWD_EXPAND)) {
15861585
CWD_STATE_FREE_ERR(&new_state);
1587-
return -1;
1586+
return FAILURE;
15881587
}
15891588

15901589
#ifdef ZEND_WIN32
1591-
retval = php_win32_ioutil_unlink(new_state.cwd);
1590+
retval = php_win32_ioutil_unlink(new_state.cwd, new_state.cwd_length);
15921591
#else
1593-
retval = unlink(new_state.cwd);
1592+
retval = virtual_unlink_native(new_state.cwd, new_state.cwd_length);
15941593
#endif
15951594

15961595
CWD_STATE_FREE_ERR(&new_state);

Zend/zend_virtual_cwd.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ CWD_API int virtual_creat(const char *path, mode_t mode);
183183
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);
186-
CWD_API int virtual_unlink(const char *path);
186+
CWD_API zend_result virtual_unlink(const char *path, size_t path_len);
187187
CWD_API int virtual_mkdir(const char *pathname, mode_t mode);
188188
CWD_API zend_result virtual_rmdir(const char *path, size_t path_len);
189189
CWD_API DIR *virtual_opendir(const char *pathname);
@@ -260,6 +260,10 @@ static zend_always_inline zend_result virtual_rename_native(const char *old_name
260260
return (rename(old_name, new_name) == 0) ? SUCCESS : FAILURE;
261261
}
262262

263+
static zend_always_inline zend_result virtual_unlink_native(const char *path, ZEND_ATTRIBUTE_UNUSED size_t path_len) {
264+
return (unlink(path) == 0) ? SUCCESS : FAILURE;
265+
}
266+
263267
static zend_always_inline zend_result virtual_rmdir_native(const char *path, ZEND_ATTRIBUTE_UNUSED size_t path_len) {
264268
return (rmdir(path) == 0) ? SUCCESS : FAILURE;
265269
}
@@ -288,7 +292,7 @@ extern void virtual_cwd_main_cwd_init(uint8_t);
288292
#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)
289293
#define VCWD_STAT(path, buff) virtual_stat(path, buff)
290294
# define VCWD_LSTAT(path, buff) virtual_lstat(path, buff)
291-
#define VCWD_UNLINK(path) virtual_unlink(path)
295+
#define VCWD_UNLINK(path, path_len) virtual_unlink(path, path_len)
292296
#define VCWD_MKDIR(path, mode) virtual_mkdir(path, mode)
293297
#define VCWD_RMDIR(path, path_len) virtual_rmdir(path, path_len)
294298
#define VCWD_OPENDIR(pathname) virtual_opendir(pathname)
@@ -317,7 +321,7 @@ extern void virtual_cwd_main_cwd_init(uint8_t);
317321
#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)
318322
#define VCWD_MKDIR(path, mode) php_win32_ioutil_mkdir(path, mode)
319323
#define VCWD_RMDIR(path, path_length) php_win32_ioutil_rmdir(path, path_length)
320-
#define VCWD_UNLINK(path) php_win32_ioutil_unlink(path)
324+
#define VCWD_UNLINK(path, path_length) php_win32_ioutil_unlink(path, path_length)
321325
#define VCWD_CHDIR(path) php_win32_ioutil_chdir(path)
322326
#define VCWD_ACCESS(pathname, mode) tsrm_win32_access(pathname, mode)
323327
#define VCWD_GETCWD(buff, size) php_win32_ioutil_getcwd(buff, size)
@@ -329,7 +333,7 @@ extern void virtual_cwd_main_cwd_init(uint8_t);
329333
#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)
330334
#define VCWD_MKDIR(path, mode) mkdir(path, mode)
331335
#define VCWD_RMDIR(path, path_len) virtual_rmdir_native(path, path_len)
332-
#define VCWD_UNLINK(path) unlink(path)
336+
#define VCWD_UNLINK(path, path_len) virtual_unlink_native(path, path_len)
333337
#define VCWD_CHDIR(path) chdir(path)
334338
#define VCWD_ACCESS(pathname, mode) access(pathname, mode)
335339
#define VCWD_GETCWD(buff, size) getcwd(buff, size)

ext/bz2/bz2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ PHP_BZ2_API php_stream *_php_stream_bz2open(php_stream_wrapper *wrapper,
231231
* failed.
232232
*/
233233
if (opened_path && !bz_file && mode[0] == 'w') {
234-
VCWD_UNLINK(ZSTR_VAL(*opened_path));
234+
VCWD_UNLINK(ZSTR_VAL(*opened_path), ZSTR_LEN(*opened_path));
235235
}
236236
}
237237

ext/ftp/php_ftp.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -682,11 +682,12 @@ PHP_FUNCTION(ftp_get)
682682
ftpbuf_t *ftp;
683683
ftptype_t xtype;
684684
php_stream *outstream;
685-
char *local, *remote;
686-
size_t local_len, remote_len;
685+
zend_string *local;
686+
char *remote;
687+
size_t remote_len;
687688
zend_long mode=FTPTYPE_IMAGE, resumepos=0;
688689

689-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Opp|ll", &z_ftp, php_ftp_ce, &local, &local_len, &remote, &remote_len, &mode, &resumepos) == FAILURE) {
690+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OPp|ll", &z_ftp, php_ftp_ce, &local, &remote, &remote_len, &mode, &resumepos) == FAILURE) {
690691
RETURN_THROWS();
691692
}
692693
GET_FTPBUF(ftp, z_ftp);
@@ -702,9 +703,9 @@ PHP_FUNCTION(ftp_get)
702703
#endif
703704

704705
if (ftp->autoseek && resumepos) {
705-
outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "rt+" : "rb+", REPORT_ERRORS, NULL);
706+
outstream = php_stream_open_wrapper(ZSTR_VAL(local), mode == FTPTYPE_ASCII ? "rt+" : "rb+", REPORT_ERRORS, NULL);
706707
if (outstream == NULL) {
707-
outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "wt" : "wb", REPORT_ERRORS, NULL);
708+
outstream = php_stream_open_wrapper(ZSTR_VAL(local), mode == FTPTYPE_ASCII ? "wt" : "wb", REPORT_ERRORS, NULL);
708709
}
709710
if (outstream != NULL) {
710711
/* if autoresume is wanted seek to end */
@@ -716,17 +717,17 @@ PHP_FUNCTION(ftp_get)
716717
}
717718
}
718719
} else {
719-
outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "wt" : "wb", REPORT_ERRORS, NULL);
720+
outstream = php_stream_open_wrapper(ZSTR_VAL(local), mode == FTPTYPE_ASCII ? "wt" : "wb", REPORT_ERRORS, NULL);
720721
}
721722

722723
if (outstream == NULL) {
723-
php_error_docref(NULL, E_WARNING, "Error opening %s", local);
724+
php_error_docref(NULL, E_WARNING, "Error opening %s", ZSTR_VAL(local));
724725
RETURN_FALSE;
725726
}
726727

727728
if (!ftp_get(ftp, outstream, remote, remote_len, xtype, resumepos)) {
728729
php_stream_close(outstream);
729-
VCWD_UNLINK(local);
730+
VCWD_UNLINK(ZSTR_VAL(local), ZSTR_LEN(local));
730731
if (*ftp->inbuf) {
731732
php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
732733
}
@@ -745,12 +746,13 @@ PHP_FUNCTION(ftp_nb_get)
745746
ftpbuf_t *ftp;
746747
ftptype_t xtype;
747748
php_stream *outstream;
748-
char *local, *remote;
749-
size_t local_len, remote_len;
749+
zend_string *local;
750+
char *remote;
751+
size_t remote_len;
750752
int ret;
751753
zend_long mode=FTPTYPE_IMAGE, resumepos=0;
752754

753-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oss|ll", &z_ftp, php_ftp_ce, &local, &local_len, &remote, &remote_len, &mode, &resumepos) == FAILURE) {
755+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OSs|ll", &z_ftp, php_ftp_ce, &local, &remote, &remote_len, &mode, &resumepos) == FAILURE) {
754756
RETURN_THROWS();
755757
}
756758
GET_FTPBUF(ftp, z_ftp);
@@ -764,9 +766,9 @@ PHP_FUNCTION(ftp_nb_get)
764766
mode = FTPTYPE_IMAGE;
765767
#endif
766768
if (ftp->autoseek && resumepos) {
767-
outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "rt+" : "rb+", REPORT_ERRORS, NULL);
769+
outstream = php_stream_open_wrapper(ZSTR_VAL(local), mode == FTPTYPE_ASCII ? "rt+" : "rb+", REPORT_ERRORS, NULL);
768770
if (outstream == NULL) {
769-
outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "wt" : "wb", REPORT_ERRORS, NULL);
771+
outstream = php_stream_open_wrapper(ZSTR_VAL(local), mode == FTPTYPE_ASCII ? "wt" : "wb", REPORT_ERRORS, NULL);
770772
}
771773
if (outstream != NULL) {
772774
/* if autoresume is wanted seek to end */
@@ -778,7 +780,7 @@ PHP_FUNCTION(ftp_nb_get)
778780
}
779781
}
780782
} else {
781-
outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "wt" : "wb", REPORT_ERRORS, NULL);
783+
outstream = php_stream_open_wrapper(ZSTR_VAL(local), mode == FTPTYPE_ASCII ? "wt" : "wb", REPORT_ERRORS, NULL);
782784
}
783785

784786
if (outstream == NULL) {
@@ -793,7 +795,7 @@ PHP_FUNCTION(ftp_nb_get)
793795
if ((ret = ftp_nb_get(ftp, outstream, remote, remote_len, xtype, resumepos)) == PHP_FTP_FAILED) {
794796
php_stream_close(outstream);
795797
ftp->stream = NULL;
796-
VCWD_UNLINK(local);
798+
VCWD_UNLINK(ZSTR_VAL(local), ZSTR_LEN(local));
797799
if (*ftp->inbuf) {
798800
php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
799801
}

ext/gd/gd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1847,7 +1847,7 @@ static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, cons
18471847
}
18481848

18491849
fclose(tmp);
1850-
VCWD_UNLINK((const char *)ZSTR_VAL(path)); /* make sure that the temporary file is removed */
1850+
VCWD_UNLINK(ZSTR_VAL(path), ZSTR_LEN(path)); /* make sure that the temporary file is removed */
18511851
zend_string_release_ex(path, 0);
18521852
}
18531853
RETURN_TRUE;

ext/session/mod_files.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,13 @@ static int ps_files_cleanup_dir(const zend_string *dirname, zend_long maxlifetim
315315
memcpy(buf + ZSTR_LEN(dirname) + 1, entry->d_name, entry_len);
316316

317317
/* NUL terminate it and */
318-
buf[ZSTR_LEN(dirname) + entry_len + 1] = '\0';
318+
size_t buf_len = ZSTR_LEN(dirname) + entry_len + 1;
319+
buf[buf_len] = '\0';
319320

320321
/* check whether its last access was more than maxlifetime ago */
321322
if (VCWD_STAT(buf, &sbuf) == 0 &&
322323
(now - sbuf.st_mtime) > maxlifetime) {
323-
VCWD_UNLINK(buf);
324+
VCWD_UNLINK(buf, buf_len);
324325
nrdels++;
325326
}
326327
}
@@ -602,7 +603,8 @@ PS_DESTROY_FUNC(files)
602603
if (data->fd != -1) {
603604
ps_files_close(data);
604605

605-
if (VCWD_UNLINK(buf) == -1) {
606+
size_t buf_len = strlen(buf);
607+
if (VCWD_UNLINK(buf, buf_len) == FAILURE) {
606608
/* This is a little safety check for instances when we are dealing with a regenerated session
607609
* that was not yet written to disk. */
608610
if (!VCWD_ACCESS(buf, F_OK)) {

ext/soap/php_sdl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2364,7 +2364,7 @@ static void add_sdl_to_cache(const char *fn, size_t fn_len, const char *uri, tim
23642364
if (valid_file) {
23652365
/* This is allowed to fail, this means that another process was raced to create the file. */
23662366
if (VCWD_RENAME(ZSTR_VAL(temp_file_path), ZSTR_LEN(temp_file_path), fn, fn_len) < 0) {
2367-
VCWD_UNLINK(ZSTR_VAL(temp_file_path));
2367+
VCWD_UNLINK(ZSTR_VAL(temp_file_path), ZSTR_LEN(temp_file_path));
23682368
}
23692369
}
23702370

ext/standard/basic_functions.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2393,7 +2393,7 @@ PHP_FUNCTION(move_uploaded_file)
23932393
}
23942394
#endif
23952395
} else if (php_copy_file_ex(ZSTR_VAL(path), new_path, STREAM_DISABLE_OPEN_BASEDIR) == SUCCESS) {
2396-
VCWD_UNLINK(ZSTR_VAL(path));
2396+
VCWD_UNLINK(ZSTR_VAL(path), ZSTR_LEN(path));
23972397
successful = 1;
23982398
}
23992399

main/rfc1867.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ PHPAPI void destroy_uploaded_files_hash(void) /* {{{ */
171171

172172
ZEND_HASH_MAP_FOREACH_VAL(SG(rfc1867_uploaded_files), el) {
173173
zend_string *filename = Z_STR_P(el);
174-
VCWD_UNLINK(ZSTR_VAL(filename));
174+
VCWD_UNLINK(ZSTR_VAL(filename), ZSTR_LEN(filename));
175175
} ZEND_HASH_FOREACH_END();
176176
zend_hash_destroy(SG(rfc1867_uploaded_files));
177177
FREE_HASHTABLE(SG(rfc1867_uploaded_files));
@@ -1089,6 +1089,7 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler)
10891089
if (cancel_upload) {
10901090
if (temp_filename) {
10911091
if (cancel_upload != PHP_UPLOAD_ERROR_E) { /* file creation failed */
1092+
// TODO Should this use VCWD_UNLINK()?
10921093
unlink(ZSTR_VAL(temp_filename));
10931094
}
10941095
zend_string_release_ex(temp_filename, 0);

main/streams/plain_wrapper.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ static int php_stdiop_close(php_stream *stream, int close_handle)
509509
}
510510
if (data->temp_name) {
511511
#ifdef PHP_WIN32
512-
php_win32_ioutil_unlink(ZSTR_VAL(data->temp_name));
512+
php_win32_ioutil_unlink(ZSTR_VAL(data->temp_name), ZSTR_LEN(data->temp_name));
513513
#else
514514
unlink(ZSTR_VAL(data->temp_name));
515515
#endif
@@ -1242,8 +1242,6 @@ static int php_plain_files_url_stater(php_stream_wrapper *wrapper, const char *u
12421242

12431243
static int php_plain_files_unlink(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context)
12441244
{
1245-
int ret;
1246-
12471245
if (strncasecmp(url, "file://", sizeof("file://") - 1) == 0) {
12481246
url += sizeof("file://") - 1;
12491247
}
@@ -1252,8 +1250,9 @@ static int php_plain_files_unlink(php_stream_wrapper *wrapper, const char *url,
12521250
return 0;
12531251
}
12541252

1255-
ret = VCWD_UNLINK(url);
1256-
if (ret == -1) {
1253+
size_t url_len = strlen(url);
1254+
zend_result ret = VCWD_UNLINK(url, url_len);
1255+
if (ret == FAILURE) {
12571256
if (options & REPORT_ERRORS) {
12581257
php_error_docref1(NULL, url, E_WARNING, "%s", strerror(errno));
12591258
}
@@ -1339,7 +1338,7 @@ static bool php_plain_files_rename(php_stream_wrapper *wrapper, const zend_strin
13391338
}
13401339
# endif
13411340
if (success) {
1342-
VCWD_UNLINK(url_from_ptr);
1341+
VCWD_UNLINK(url_from_ptr, url_from_len);
13431342
}
13441343
} else {
13451344
php_error_docref2(NULL, ZSTR_VAL(url_from), ZSTR_VAL(url_to), E_WARNING, "%s", strerror(errno));

win32/ioutil.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -365,15 +365,15 @@ PW32IO int php_win32_ioutil_mkdir_w(const wchar_t *path, mode_t mode)
365365
return 0;
366366
}/*}}}*/
367367

368-
PW32IO int php_win32_ioutil_unlink_w(const wchar_t *path)
368+
PW32IO zend_result php_win32_ioutil_unlink_w(const wchar_t *path)
369369
{/*{{{*/
370370
DWORD err = 0;
371371
HANDLE h;
372372
BY_HANDLE_FILE_INFORMATION info;
373373
FILE_DISPOSITION_INFO disposition;
374374
BOOL status;
375375

376-
PHP_WIN32_IOUTIL_CHECK_PATH_W(path, -1, 0)
376+
PHP_WIN32_IOUTIL_CHECK_PATH_W(path, FAILURE, 0)
377377

378378
h = CreateFileW(path,
379379
FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | DELETE,
@@ -386,21 +386,21 @@ PW32IO int php_win32_ioutil_unlink_w(const wchar_t *path)
386386
if (INVALID_HANDLE_VALUE == h) {
387387
err = GetLastError();
388388
SET_ERRNO_FROM_WIN32_CODE(err);
389-
return -1;
389+
return FAILURE;
390390
}
391391

392392
if (!GetFileInformationByHandle(h, &info)) {
393393
err = GetLastError();
394394
CloseHandle(h);
395395
SET_ERRNO_FROM_WIN32_CODE(err);
396-
return -1;
396+
return FAILURE;
397397
}
398398

399399
if (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
400400
/* TODO Handle possible reparse point. */
401401
CloseHandle(h);
402402
SET_ERRNO_FROM_WIN32_CODE(ERROR_DIRECTORY_NOT_SUPPORTED);
403-
return -1;
403+
return FAILURE;
404404
}
405405

406406
#if 0
@@ -428,12 +428,12 @@ PW32IO int php_win32_ioutil_unlink_w(const wchar_t *path)
428428
err = GetLastError();
429429
CloseHandle(h);
430430
SET_ERRNO_FROM_WIN32_CODE(err);
431-
return -1;
431+
return FAILURE;
432432
}
433433

434434
CloseHandle(h);
435435

436-
return 0;
436+
return SUCCESS;
437437
}/*}}}*/
438438

439439
PW32IO int php_win32_ioutil_chdir_w(const wchar_t *path)

win32/ioutil.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ 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);
267267
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);
269-
PW32IO int php_win32_ioutil_unlink_w(const wchar_t *path);
269+
PW32IO zend_result php_win32_ioutil_unlink_w(const wchar_t *path);
270270
PW32IO int php_win32_ioutil_access_w(const wchar_t *path, mode_t mode);
271271
PW32IO int php_win32_ioutil_mkdir_w(const wchar_t *path, mode_t mode);
272272
PW32IO FILE *php_win32_ioutil_fopen_w(const wchar_t *path, const wchar_t *mode);
@@ -335,26 +335,21 @@ __forceinline static int php_win32_ioutil_open(const char *path, int flags, ...)
335335
return ret;
336336
}/*}}}*/
337337

338-
__forceinline static int php_win32_ioutil_unlink(const char *path)
338+
__forceinline static zend_result php_win32_ioutil_unlink(const char *path, size_t path_len)
339339
{/*{{{*/
340340
PHP_WIN32_IOUTIL_INIT_W(path)
341-
int ret = -1;
342-
DWORD err;
343341

344342
if (!pathw) {
345343
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
346-
return -1;
347-
}
348-
349-
ret = php_win32_ioutil_unlink_w(pathw);
350-
if (0 > ret) {
351-
err = GetLastError();
344+
return FAILURE;
352345
}
353-
PHP_WIN32_IOUTIL_CLEANUP_W()
354346

355-
if (0 > ret) {
347+
zend_result ret = php_win32_ioutil_unlink_w(pathw);
348+
if (ret == FAILURE) {
349+
DWORD err = GetLastError();
356350
SET_ERRNO_FROM_WIN32_CODE(err);
357351
}
352+
PHP_WIN32_IOUTIL_CLEANUP_W()
358353

359354
return ret;
360355
}/*}}}*/

0 commit comments

Comments
 (0)