Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Zend: Refactor virtual_unlink() to take path lengths
  • Loading branch information
Girgias committed Aug 23, 2024
commit 947c4028a0e1f5fb7f1930485d1efe1260ffbc75
11 changes: 5 additions & 6 deletions Zend/zend_virtual_cwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1573,22 +1573,21 @@ CWD_API int virtual_lstat(const char *path, zend_stat_t *buf) /* {{{ */
}
/* }}} */

CWD_API int virtual_unlink(const char *path) /* {{{ */
CWD_API zend_result virtual_unlink(const char *path, size_t path_length) /* {{{ */
{
cwd_state new_state;
int retval;
size_t path_length = strlen(path);
zend_result retval;

CWD_STATE_COPY(&new_state, &CWDG(cwd));
if (virtual_file_ex(&new_state, path, path_length, NULL, CWD_EXPAND)) {
CWD_STATE_FREE_ERR(&new_state);
return -1;
return FAILURE;
}

#ifdef ZEND_WIN32
retval = php_win32_ioutil_unlink(new_state.cwd);
retval = php_win32_ioutil_unlink(new_state.cwd, new_state.cwd_length);
#else
retval = unlink(new_state.cwd);
retval = virtual_unlink_native(new_state.cwd, new_state.cwd_length);
#endif

CWD_STATE_FREE_ERR(&new_state);
Expand Down
12 changes: 8 additions & 4 deletions Zend/zend_virtual_cwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ CWD_API int virtual_creat(const char *path, mode_t mode);
CWD_API zend_result virtual_rename(const char *old_name, size_t old_name_len, const char *new_name, size_t new_name_len);
CWD_API int virtual_stat(const char *path, zend_stat_t *buf);
CWD_API int virtual_lstat(const char *path, zend_stat_t *buf);
CWD_API int virtual_unlink(const char *path);
CWD_API zend_result virtual_unlink(const char *path, size_t path_len);
CWD_API int virtual_mkdir(const char *pathname, mode_t mode);
CWD_API zend_result virtual_rmdir(const char *path, size_t path_len);
CWD_API DIR *virtual_opendir(const char *pathname);
Expand Down Expand Up @@ -260,6 +260,10 @@ static zend_always_inline zend_result virtual_rename_native(const char *old_name
return (rename(old_name, new_name) == 0) ? SUCCESS : FAILURE;
}

static zend_always_inline zend_result virtual_unlink_native(const char *path, ZEND_ATTRIBUTE_UNUSED size_t path_len) {
return (unlink(path) == 0) ? SUCCESS : FAILURE;
}

static zend_always_inline zend_result virtual_rmdir_native(const char *path, ZEND_ATTRIBUTE_UNUSED size_t path_len) {
return (rmdir(path) == 0) ? SUCCESS : FAILURE;
}
Expand Down Expand Up @@ -288,7 +292,7 @@ extern void virtual_cwd_main_cwd_init(uint8_t);
#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)
#define VCWD_STAT(path, buff) virtual_stat(path, buff)
# define VCWD_LSTAT(path, buff) virtual_lstat(path, buff)
#define VCWD_UNLINK(path) virtual_unlink(path)
#define VCWD_UNLINK(path, path_len) virtual_unlink(path, path_len)
#define VCWD_MKDIR(path, mode) virtual_mkdir(path, mode)
#define VCWD_RMDIR(path, path_len) virtual_rmdir(path, path_len)
#define VCWD_OPENDIR(pathname) virtual_opendir(pathname)
Expand Down Expand Up @@ -317,7 +321,7 @@ extern void virtual_cwd_main_cwd_init(uint8_t);
#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)
#define VCWD_MKDIR(path, mode) php_win32_ioutil_mkdir(path, mode)
#define VCWD_RMDIR(path, path_length) php_win32_ioutil_rmdir(path, path_length)
#define VCWD_UNLINK(path) php_win32_ioutil_unlink(path)
#define VCWD_UNLINK(path, path_length) php_win32_ioutil_unlink(path, path_length)
#define VCWD_CHDIR(path) php_win32_ioutil_chdir(path)
#define VCWD_ACCESS(pathname, mode) tsrm_win32_access(pathname, mode)
#define VCWD_GETCWD(buff, size) php_win32_ioutil_getcwd(buff, size)
Expand All @@ -329,7 +333,7 @@ extern void virtual_cwd_main_cwd_init(uint8_t);
#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)
#define VCWD_MKDIR(path, mode) mkdir(path, mode)
#define VCWD_RMDIR(path, path_len) virtual_rmdir_native(path, path_len)
#define VCWD_UNLINK(path) unlink(path)
#define VCWD_UNLINK(path, path_len) virtual_unlink_native(path, path_len)
#define VCWD_CHDIR(path) chdir(path)
#define VCWD_ACCESS(pathname, mode) access(pathname, mode)
#define VCWD_GETCWD(buff, size) getcwd(buff, size)
Expand Down
2 changes: 1 addition & 1 deletion ext/bz2/bz2.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ PHP_BZ2_API php_stream *_php_stream_bz2open(php_stream_wrapper *wrapper,
* failed.
*/
if (opened_path && !bz_file && mode[0] == 'w') {
VCWD_UNLINK(ZSTR_VAL(*opened_path));
VCWD_UNLINK(ZSTR_VAL(*opened_path), ZSTR_LEN(*opened_path));
}
}

Expand Down
34 changes: 18 additions & 16 deletions ext/ftp/php_ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -682,11 +682,12 @@ PHP_FUNCTION(ftp_get)
ftpbuf_t *ftp;
ftptype_t xtype;
php_stream *outstream;
char *local, *remote;
size_t local_len, remote_len;
zend_string *local;
char *remote;
size_t remote_len;
zend_long mode=FTPTYPE_IMAGE, resumepos=0;

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

if (ftp->autoseek && resumepos) {
outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "rt+" : "rb+", REPORT_ERRORS, NULL);
outstream = php_stream_open_wrapper(ZSTR_VAL(local), mode == FTPTYPE_ASCII ? "rt+" : "rb+", REPORT_ERRORS, NULL);
if (outstream == NULL) {
outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "wt" : "wb", REPORT_ERRORS, NULL);
outstream = php_stream_open_wrapper(ZSTR_VAL(local), mode == FTPTYPE_ASCII ? "wt" : "wb", REPORT_ERRORS, NULL);
}
if (outstream != NULL) {
/* if autoresume is wanted seek to end */
Expand All @@ -716,17 +717,17 @@ PHP_FUNCTION(ftp_get)
}
}
} else {
outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "wt" : "wb", REPORT_ERRORS, NULL);
outstream = php_stream_open_wrapper(ZSTR_VAL(local), mode == FTPTYPE_ASCII ? "wt" : "wb", REPORT_ERRORS, NULL);
}

if (outstream == NULL) {
php_error_docref(NULL, E_WARNING, "Error opening %s", local);
php_error_docref(NULL, E_WARNING, "Error opening %s", ZSTR_VAL(local));
RETURN_FALSE;
}

if (!ftp_get(ftp, outstream, remote, remote_len, xtype, resumepos)) {
php_stream_close(outstream);
VCWD_UNLINK(local);
VCWD_UNLINK(ZSTR_VAL(local), ZSTR_LEN(local));
if (*ftp->inbuf) {
php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
}
Expand All @@ -745,12 +746,13 @@ PHP_FUNCTION(ftp_nb_get)
ftpbuf_t *ftp;
ftptype_t xtype;
php_stream *outstream;
char *local, *remote;
size_t local_len, remote_len;
zend_string *local;
char *remote;
size_t remote_len;
int ret;
zend_long mode=FTPTYPE_IMAGE, resumepos=0;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oss|ll", &z_ftp, php_ftp_ce, &local, &local_len, &remote, &remote_len, &mode, &resumepos) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OSs|ll", &z_ftp, php_ftp_ce, &local, &remote, &remote_len, &mode, &resumepos) == FAILURE) {
RETURN_THROWS();
}
GET_FTPBUF(ftp, z_ftp);
Expand All @@ -764,9 +766,9 @@ PHP_FUNCTION(ftp_nb_get)
mode = FTPTYPE_IMAGE;
#endif
if (ftp->autoseek && resumepos) {
outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "rt+" : "rb+", REPORT_ERRORS, NULL);
outstream = php_stream_open_wrapper(ZSTR_VAL(local), mode == FTPTYPE_ASCII ? "rt+" : "rb+", REPORT_ERRORS, NULL);
if (outstream == NULL) {
outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "wt" : "wb", REPORT_ERRORS, NULL);
outstream = php_stream_open_wrapper(ZSTR_VAL(local), mode == FTPTYPE_ASCII ? "wt" : "wb", REPORT_ERRORS, NULL);
}
if (outstream != NULL) {
/* if autoresume is wanted seek to end */
Expand All @@ -778,11 +780,11 @@ PHP_FUNCTION(ftp_nb_get)
}
}
} else {
outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "wt" : "wb", REPORT_ERRORS, NULL);
outstream = php_stream_open_wrapper(ZSTR_VAL(local), mode == FTPTYPE_ASCII ? "wt" : "wb", REPORT_ERRORS, NULL);
}

if (outstream == NULL) {
php_error_docref(NULL, E_WARNING, "Error opening %s", local);
php_error_docref(NULL, E_WARNING, "Error opening %s", ZSTR_VAL(local));
RETURN_FALSE;
}

Expand All @@ -793,7 +795,7 @@ PHP_FUNCTION(ftp_nb_get)
if ((ret = ftp_nb_get(ftp, outstream, remote, remote_len, xtype, resumepos)) == PHP_FTP_FAILED) {
php_stream_close(outstream);
ftp->stream = NULL;
VCWD_UNLINK(local);
VCWD_UNLINK(ZSTR_VAL(local), ZSTR_LEN(local));
if (*ftp->inbuf) {
php_error_docref(NULL, E_WARNING, "%s", ftp->inbuf);
}
Expand Down
2 changes: 1 addition & 1 deletion ext/gd/gd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1847,7 +1847,7 @@ static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, cons
}

fclose(tmp);
VCWD_UNLINK((const char *)ZSTR_VAL(path)); /* make sure that the temporary file is removed */
VCWD_UNLINK(ZSTR_VAL(path), ZSTR_LEN(path)); /* make sure that the temporary file is removed */
zend_string_release_ex(path, 0);
}
RETURN_TRUE;
Expand Down
24 changes: 12 additions & 12 deletions ext/opcache/zend_file_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
#endif

#ifndef ZEND_WIN32
#define zend_file_cache_unlink unlink
#define zend_file_cache_unlink virtual_unlink_native
#define zend_file_cache_open open
#else
#define zend_file_cache_unlink php_win32_ioutil_unlink
Expand Down Expand Up @@ -1161,7 +1161,7 @@ int zend_file_cache_script_store(zend_persistent_script *script, bool in_shm)
zend_string_release_ex(s, 0);
close(fd);
efree(mem);
zend_file_cache_unlink(filename);
zend_file_cache_unlink(filename, strlen(filename));
efree(filename);
return FAILURE;
}
Expand Down Expand Up @@ -1826,6 +1826,7 @@ zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handl
return NULL;
}
filename = zend_file_cache_get_bin_file_path(full_path);
size_t filename_len = strlen(filename);

fd = zend_file_cache_open(filename, O_RDONLY | O_BINARY);
if (fd < 0) {
Expand All @@ -1843,7 +1844,7 @@ zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handl
zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot read from file '%s' (info)\n", filename);
zend_file_cache_flock(fd, LOCK_UN);
close(fd);
zend_file_cache_unlink(filename);
zend_file_cache_unlink(filename, filename_len);
efree(filename);
return NULL;
}
Expand All @@ -1853,15 +1854,15 @@ zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handl
zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot read from file '%s' (wrong header)\n", filename);
zend_file_cache_flock(fd, LOCK_UN);
close(fd);
zend_file_cache_unlink(filename);
zend_file_cache_unlink(filename, filename_len);
efree(filename);
return NULL;
}
if (memcmp(info.system_id, zend_system_id, 32) != 0) {
zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot read from file '%s' (wrong \"system_id\")\n", filename);
zend_file_cache_flock(fd, LOCK_UN);
close(fd);
zend_file_cache_unlink(filename);
zend_file_cache_unlink(filename, filename_len);
efree(filename);
return NULL;
}
Expand All @@ -1873,7 +1874,7 @@ zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handl
zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot unlock file '%s'\n", filename);
}
close(fd);
zend_file_cache_unlink(filename);
zend_file_cache_unlink(filename, filename_len);
efree(filename);
return NULL;
}
Expand All @@ -1891,7 +1892,7 @@ zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handl
zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot read from file '%s' (mem)\n", filename);
zend_file_cache_flock(fd, LOCK_UN);
close(fd);
zend_file_cache_unlink(filename);
zend_file_cache_unlink(filename, filename_len);
zend_arena_release(&CG(arena), checkpoint);
efree(filename);
return NULL;
Expand All @@ -1905,7 +1906,7 @@ zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handl
if (ZCG(accel_directives).file_cache_consistency_checks &&
(actual_checksum = zend_adler32(ADLER32_INIT, mem, info.mem_size + info.str_size)) != info.checksum) {
zend_accel_error(ACCEL_LOG_WARNING, "corrupted file '%s' excepted checksum: 0x%08x actual checksum: 0x%08x\n", filename, info.checksum, actual_checksum);
zend_file_cache_unlink(filename);
zend_file_cache_unlink(filename, filename_len);
zend_arena_release(&CG(arena), checkpoint);
efree(filename);
return NULL;
Expand Down Expand Up @@ -1997,10 +1998,9 @@ zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handl

void zend_file_cache_invalidate(zend_string *full_path)
{
char *filename;

filename = zend_file_cache_get_bin_file_path(full_path);
char *filename = zend_file_cache_get_bin_file_path(full_path);
size_t filename_len = strlen(filename);

zend_file_cache_unlink(filename);
zend_file_cache_unlink(filename, filename_len);
efree(filename);
}
8 changes: 5 additions & 3 deletions ext/session/mod_files.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,13 @@ static int ps_files_cleanup_dir(const zend_string *dirname, zend_long maxlifetim
memcpy(buf + ZSTR_LEN(dirname) + 1, entry->d_name, entry_len);

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

/* check whether its last access was more than maxlifetime ago */
if (VCWD_STAT(buf, &sbuf) == 0 &&
(now - sbuf.st_mtime) > maxlifetime) {
VCWD_UNLINK(buf);
VCWD_UNLINK(buf, buf_len);
nrdels++;
}
}
Expand Down Expand Up @@ -602,7 +603,8 @@ PS_DESTROY_FUNC(files)
if (data->fd != -1) {
ps_files_close(data);

if (VCWD_UNLINK(buf) == -1) {
size_t buf_len = strlen(buf);
if (VCWD_UNLINK(buf, buf_len) == FAILURE) {
/* This is a little safety check for instances when we are dealing with a regenerated session
* that was not yet written to disk. */
if (!VCWD_ACCESS(buf, F_OK)) {
Expand Down
2 changes: 1 addition & 1 deletion ext/soap/php_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2364,7 +2364,7 @@ static void add_sdl_to_cache(const char *fn, size_t fn_len, const char *uri, tim
if (valid_file) {
/* This is allowed to fail, this means that another process was raced to create the file. */
if (VCWD_RENAME(ZSTR_VAL(temp_file_path), ZSTR_LEN(temp_file_path), fn, fn_len) < 0) {
VCWD_UNLINK(ZSTR_VAL(temp_file_path));
VCWD_UNLINK(ZSTR_VAL(temp_file_path), ZSTR_LEN(temp_file_path));
}
}

Expand Down
2 changes: 1 addition & 1 deletion ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -2393,7 +2393,7 @@ PHP_FUNCTION(move_uploaded_file)
}
#endif
} else if (php_copy_file_ex(ZSTR_VAL(path), new_path, STREAM_DISABLE_OPEN_BASEDIR) == SUCCESS) {
VCWD_UNLINK(ZSTR_VAL(path));
VCWD_UNLINK(ZSTR_VAL(path), ZSTR_LEN(path));
successful = 1;
}

Expand Down
3 changes: 2 additions & 1 deletion main/rfc1867.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ PHPAPI void destroy_uploaded_files_hash(void) /* {{{ */

ZEND_HASH_MAP_FOREACH_VAL(SG(rfc1867_uploaded_files), el) {
zend_string *filename = Z_STR_P(el);
VCWD_UNLINK(ZSTR_VAL(filename));
VCWD_UNLINK(ZSTR_VAL(filename), ZSTR_LEN(filename));
} ZEND_HASH_FOREACH_END();
zend_hash_destroy(SG(rfc1867_uploaded_files));
FREE_HASHTABLE(SG(rfc1867_uploaded_files));
Expand Down Expand Up @@ -1089,6 +1089,7 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler)
if (cancel_upload) {
if (temp_filename) {
if (cancel_upload != PHP_UPLOAD_ERROR_E) { /* file creation failed */
// TODO Should this use VCWD_UNLINK()?
unlink(ZSTR_VAL(temp_filename));
}
zend_string_release_ex(temp_filename, 0);
Expand Down
11 changes: 5 additions & 6 deletions main/streams/plain_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ static int php_stdiop_close(php_stream *stream, int close_handle)
}
if (data->temp_name) {
#ifdef PHP_WIN32
php_win32_ioutil_unlink(ZSTR_VAL(data->temp_name));
php_win32_ioutil_unlink(ZSTR_VAL(data->temp_name), ZSTR_LEN(data->temp_name));
#else
unlink(ZSTR_VAL(data->temp_name));
#endif
Expand Down Expand Up @@ -1242,8 +1242,6 @@ static int php_plain_files_url_stater(php_stream_wrapper *wrapper, const char *u

static int php_plain_files_unlink(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context)
{
int ret;

if (strncasecmp(url, "file://", sizeof("file://") - 1) == 0) {
url += sizeof("file://") - 1;
}
Expand All @@ -1252,8 +1250,9 @@ static int php_plain_files_unlink(php_stream_wrapper *wrapper, const char *url,
return 0;
}

ret = VCWD_UNLINK(url);
if (ret == -1) {
size_t url_len = strlen(url);
zend_result ret = VCWD_UNLINK(url, url_len);
if (ret == FAILURE) {
if (options & REPORT_ERRORS) {
php_error_docref1(NULL, url, E_WARNING, "%s", strerror(errno));
}
Expand Down Expand Up @@ -1339,7 +1338,7 @@ static bool php_plain_files_rename(php_stream_wrapper *wrapper, const zend_strin
}
# endif
if (success) {
VCWD_UNLINK(url_from_ptr);
VCWD_UNLINK(url_from_ptr, url_from_len);
}
} else {
php_error_docref2(NULL, ZSTR_VAL(url_from), ZSTR_VAL(url_to), E_WARNING, "%s", strerror(errno));
Expand Down
Loading