summaryrefslogtreecommitdiff
path: root/main
diff options
context:
space:
mode:
authorAnatol Belski <ab@php.net>2017-07-26 23:10:07 +0200
committerAnatol Belski <ab@php.net>2017-07-27 20:11:21 +0200
commit49d9b3013fb2ee18b59694aa36036104a4bdd6f2 (patch)
tree40da76daa5117b1ba623c3e6fcc5c4d44ce94777 /main
parent00bed31d9f24ce46b461e911c06d7a8b6e2cf1c0 (diff)
downloadphp-git-49d9b3013fb2ee18b59694aa36036104a4bdd6f2.tar.gz
Move cwd_state and path related routines to size_t
Having `int` there is no real profit in the size or speed, while unsigned improves security and overall integration. ZPP supplied strings can be then accepted directly and structs can be still handled with smaller unsigned types for size reasons, which is safe. Yet some related places are to go. basic move tsrm_realpath_r to size_t fix conditions and sync with affected places touch ocurrences of php_sys_readlink usage follow up on phar path handling remove duplicated check move zend_resolve_path and related pieces to size_t touch yet resolve path related places remove cast missing pieces missing piece yet cleanups for php_sys_readlink for ssize_t fix wrong return
Diffstat (limited to 'main')
-rw-r--r--main/fopen_wrappers.c32
-rw-r--r--main/fopen_wrappers.h2
-rw-r--r--main/main.c2
-rw-r--r--main/php_open_temporary_file.c11
-rw-r--r--main/streams/streams.c2
5 files changed, 26 insertions, 23 deletions
diff --git a/main/fopen_wrappers.c b/main/fopen_wrappers.c
index 12de33be83..06b0eb35fa 100644
--- a/main/fopen_wrappers.c
+++ b/main/fopen_wrappers.c
@@ -168,11 +168,11 @@ PHPAPI int php_check_specific_open_basedir(const char *basedir, const char *path
while (VCWD_REALPATH(path_tmp, resolved_name) == NULL) {
#if defined(PHP_WIN32) || defined(HAVE_SYMLINK)
if (nesting_level == 0) {
- int ret;
+ ssize_t ret;
char buf[MAXPATHLEN];
ret = php_sys_readlink(path_tmp, buf, MAXPATHLEN - 1);
- if (ret < 0) {
+ if (ret == -1) {
/* not a broken symlink, move along.. */
} else {
/* put the real path into the path buffer */
@@ -355,7 +355,7 @@ PHPAPI int php_fopen_primary_script(zend_file_handle *file_handle)
char *path_info;
char *filename = NULL;
zend_string *resolved_path = NULL;
- int length;
+ size_t length;
zend_bool orig_display_errors;
path_info = SG(request_info).request_uri;
@@ -378,7 +378,7 @@ PHPAPI int php_fopen_primary_script(zend_file_handle *file_handle)
pwbuf = emalloc(pwbuflen);
#endif
length = s - (path_info + 2);
- if (length > (int)sizeof(user) - 1) {
+ if (length > sizeof(user) - 1) {
length = sizeof(user) - 1;
}
memcpy(user, path_info + 2, length);
@@ -402,9 +402,9 @@ PHPAPI int php_fopen_primary_script(zend_file_handle *file_handle)
}
} else
#endif
- if (PG(doc_root) && path_info && (length = (int)strlen(PG(doc_root))) &&
+ if (PG(doc_root) && path_info && (length = strlen(PG(doc_root))) &&
IS_ABSOLUTE_PATH(PG(doc_root), length)) {
- int path_len = (int)strlen(path_info);
+ size_t path_len = strlen(path_info);
filename = emalloc(length + path_len + 2);
memcpy(filename, PG(doc_root), length);
if (!IS_SLASH(filename[length - 1])) { /* length is never 0 */
@@ -420,7 +420,7 @@ PHPAPI int php_fopen_primary_script(zend_file_handle *file_handle)
if (filename) {
- resolved_path = zend_resolve_path(filename, (int)strlen(filename));
+ resolved_path = zend_resolve_path(filename, strlen(filename));
}
if (!resolved_path) {
@@ -472,7 +472,7 @@ PHPAPI int php_fopen_primary_script(zend_file_handle *file_handle)
/* {{{ php_resolve_path
* Returns the realpath for given filename according to include path
*/
-PHPAPI zend_string *php_resolve_path(const char *filename, int filename_length, const char *path)
+PHPAPI zend_string *php_resolve_path(const char *filename, size_t filename_length, const char *path)
{
char resolved_path[MAXPATHLEN];
char trypath[MAXPATHLEN];
@@ -532,7 +532,7 @@ PHPAPI zend_string *php_resolve_path(const char *filename, int filename_length,
}
end = strchr(p, DEFAULT_DIR_SEPARATOR);
if (end) {
- if (filename_length > (MAXPATHLEN - 2) || (end-ptr) > MAXPATHLEN || (end-ptr) + 1 + (size_t)filename_length + 1 >= MAXPATHLEN) {
+ if (filename_length > (MAXPATHLEN - 2) || (end-ptr) > MAXPATHLEN || (end-ptr) + 1 + filename_length + 1 >= MAXPATHLEN) {
ptr = end + 1;
continue;
}
@@ -543,7 +543,7 @@ PHPAPI zend_string *php_resolve_path(const char *filename, int filename_length,
} else {
size_t len = strlen(ptr);
- if (filename_length > (MAXPATHLEN - 2) || len > MAXPATHLEN || len + 1 + (size_t)filename_length + 1 >= MAXPATHLEN) {
+ if (filename_length > (MAXPATHLEN - 2) || len > MAXPATHLEN || len + 1 + filename_length + 1 >= MAXPATHLEN) {
break;
}
memcpy(trypath, ptr, len);
@@ -624,7 +624,7 @@ PHPAPI FILE *php_fopen_with_path(const char *filename, const char *mode, const c
char *pathbuf, *ptr, *end;
char trypath[MAXPATHLEN];
FILE *fp;
- int filename_length;
+ size_t filename_length;
zend_string *exec_filename;
if (opened_path) {
@@ -635,7 +635,7 @@ PHPAPI FILE *php_fopen_with_path(const char *filename, const char *mode, const c
return NULL;
}
- filename_length = (int)strlen(filename);
+ filename_length = strlen(filename);
#ifndef PHP_WIN32
(void) filename_length;
#endif
@@ -761,14 +761,14 @@ PHPAPI char *expand_filepath_with_mode(const char *filepath, char *real_path, co
{
cwd_state new_state;
char cwd[MAXPATHLEN];
- int copy_len;
- int path_len;
+ size_t copy_len;
+ size_t path_len;
if (!filepath[0]) {
return NULL;
}
- path_len = (int)strlen(filepath);
+ path_len = strlen(filepath);
if (IS_ABSOLUTE_PATH(filepath, path_len)) {
cwd[0] = '\0';
@@ -811,7 +811,7 @@ PHPAPI char *expand_filepath_with_mode(const char *filepath, char *real_path, co
}
new_state.cwd = estrdup(cwd);
- new_state.cwd_length = (int)strlen(cwd);
+ new_state.cwd_length = strlen(cwd);
if (virtual_file_ex(&new_state, filepath, NULL, realpath_mode)) {
efree(new_state.cwd);
diff --git a/main/fopen_wrappers.h b/main/fopen_wrappers.h
index 5e1544c513..cdded57938 100644
--- a/main/fopen_wrappers.h
+++ b/main/fopen_wrappers.h
@@ -39,7 +39,7 @@ PHPAPI int php_check_specific_open_basedir(const char *basedir, const char *path
PHPAPI int php_check_safe_mode_include_dir(const char *path);
-PHPAPI zend_string *php_resolve_path(const char *filename, int filename_len, const char *path);
+PHPAPI zend_string *php_resolve_path(const char *filename, size_t filename_len, const char *path);
PHPAPI FILE *php_fopen_with_path(const char *filename, const char *mode, const char *path, zend_string **opened_path);
diff --git a/main/main.c b/main/main.c
index 25fa644e4d..360082fc5e 100644
--- a/main/main.c
+++ b/main/main.c
@@ -1427,7 +1427,7 @@ PHPAPI int php_stream_open_for_zend_ex(const char *filename, zend_file_handle *h
}
/* }}} */
-static zend_string *php_resolve_path_for_zend(const char *filename, int filename_len) /* {{{ */
+static zend_string *php_resolve_path_for_zend(const char *filename, size_t filename_len) /* {{{ */
{
return php_resolve_path(filename, filename_len, PG(include_path));
}
diff --git a/main/php_open_temporary_file.c b/main/php_open_temporary_file.c
index f2303882f4..71354654b5 100644
--- a/main/php_open_temporary_file.c
+++ b/main/php_open_temporary_file.c
@@ -125,7 +125,7 @@ static int php_do_open_temporary_file(const char *path, const char *pfx, zend_st
}
new_state.cwd = estrdup(cwd);
- new_state.cwd_length = (int)strlen(cwd);
+ new_state.cwd_length = strlen(cwd);
if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH)) {
efree(new_state.cwd);
@@ -216,7 +216,7 @@ PHPAPI const char* php_get_temporary_directory(void)
{
char *sys_temp_dir = PG(sys_temp_dir);
if (sys_temp_dir) {
- int len = (int)strlen(sys_temp_dir);
+ size_t len = strlen(sys_temp_dir);
if (len >= 2 && sys_temp_dir[len - 1] == DEFAULT_SLASH) {
PG(php_sys_temp_dir) = estrndup(sys_temp_dir, len - 1);
return PG(php_sys_temp_dir);
@@ -237,7 +237,10 @@ PHPAPI const char* php_get_temporary_directory(void)
wchar_t sTemp[MAXPATHLEN];
char *tmp;
size_t len = GetTempPathW(MAXPATHLEN, sTemp);
- assert(0 < len); /* should *never* fail! */
+
+ if (!len) {
+ return NULL;
+ }
if (NULL == (tmp = php_win32_ioutil_conv_w_to_any(sTemp, len, &len))) {
return NULL;
@@ -253,7 +256,7 @@ PHPAPI const char* php_get_temporary_directory(void)
{
char* s = getenv("TMPDIR");
if (s && *s) {
- int len = strlen(s);
+ size_t len = strlen(s);
if (s[len - 1] == DEFAULT_SLASH) {
PG(php_sys_temp_dir) = estrndup(s, len - 1);
diff --git a/main/streams/streams.c b/main/streams/streams.c
index dab8505d41..e768d0f533 100644
--- a/main/streams/streams.c
+++ b/main/streams/streams.c
@@ -1996,7 +1996,7 @@ PHPAPI php_stream *_php_stream_open_wrapper_ex(const char *path, const char *mod
}
if (options & USE_PATH) {
- resolved_path = zend_resolve_path(path, (int)strlen(path));
+ resolved_path = zend_resolve_path(path, strlen(path));
if (resolved_path) {
path = ZSTR_VAL(resolved_path);
/* we've found this file, don't re-check include_path or run realpath */