com php-src: fix and improve the ioutil cwd routine for nts: win32/ioutil.c win32/ioutil.h
| From: | Anatol Belski | Date: | Fri, 29 Jul 2016 00:41:00 +0000 |
| Subject: | com php-src: fix and improve the ioutil cwd routine for nts: win32/ioutil.c win32/ioutil.h | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-93469@lists.php.net to get a copy of this message | ||
Commit: 41d172d20f3c010f43468b1be3e32826fec0fa31
Author: Anatol Belski <ab@php.net> Fri, 29 Jul 2016 02:13:06 +0200
Parents: 09b5a453c1915b84b46e1c6e5687a2e1bc5bad22
Branches: PHP-7.1 master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=41d172d20f3c010f43468b1be3e32826fec0fa31
Log:
fix and improve the ioutil cwd routine for nts
- move intermediate var to stack
- fix length calculation
- improve error code setting
Changed paths:
M win32/ioutil.c
M win32/ioutil.h
Diff:
diff --git a/win32/ioutil.c b/win32/ioutil.c
index fc4413b..b661a15 100644
--- a/win32/ioutil.c
+++ b/win32/ioutil.c
@@ -425,6 +425,7 @@ PW32IO wchar_t *php_win32_ioutil_getcwd_w(const wchar_t *buf, int len)
if (!GetCurrentDirectoryW(len, buf)) {
err = GetLastError();
SET_ERRNO_FROM_WIN32_CODE(err);
+ return NULL;
}
return (wchar_t *)buf;
diff --git a/win32/ioutil.h b/win32/ioutil.h
index 2c96928..d2e8239 100644
--- a/win32/ioutil.h
+++ b/win32/ioutil.h
@@ -417,27 +417,28 @@ __forceinline static int php_win32_ioutil_chdir(const char *patha)
__forceinline static char *php_win32_ioutil_getcwd(char *buf, int len)
{/*{{{*/
- wchar_t *tmp_bufw = NULL;
+ wchar_t tmp_bufw[PHP_WIN32_IOUTIL_MAXPATHLEN];
char *tmp_bufa = NULL;
+ size_t tmp_bufa_len;
DWORD err = 0;
- tmp_bufw = php_win32_ioutil_getcwd_w(tmp_bufw, len);
- if (!tmp_bufw) {
+ if (php_win32_ioutil_getcwd_w(tmp_bufw, PHP_WIN32_IOUTIL_MAXPATHLEN) == NULL) {
err = GetLastError();
SET_ERRNO_FROM_WIN32_CODE(err);
return NULL;
}
- tmp_bufa = php_win32_ioutil_w_to_any(tmp_bufw);
+ tmp_bufa = php_win32_cp_conv_w_to_any(tmp_bufw, wcslen(tmp_bufw), &tmp_bufa_len);
if (!tmp_bufa) {
err = GetLastError();
- buf = NULL;
- free(tmp_bufw);
SET_ERRNO_FROM_WIN32_CODE(err);
- return buf;
- } else if ((int)strlen(tmp_bufa) > len) {
+ return NULL;
+ } else if (tmp_bufa_len + 1 > PHP_WIN32_IOUTIL_MAXPATHLEN) {
+ free(tmp_bufa);
+ SET_ERRNO_FROM_WIN32_CODE(ERROR_BAD_LENGTH);
+ return NULL;
+ } else if (tmp_bufa_len + 1 > len) {
free(tmp_bufa);
- free(tmp_bufw);
SET_ERRNO_FROM_WIN32_CODE(ERROR_INSUFFICIENT_BUFFER);
return NULL;
}
@@ -446,12 +447,10 @@ __forceinline static char *php_win32_ioutil_getcwd(char *buf, int len)
/* If buf was NULL, the result has to be freed outside here. */
buf = tmp_bufa;
} else {
- memmove(buf, tmp_bufa, len);
+ memmove(buf, tmp_bufa, tmp_bufa_len + 1);
free(tmp_bufa);
}
- free(tmp_bufw);
-
return buf;
}/*}}}*/