1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

fix and improve the ioutil cwd routine for nts

- move intermediate var to stack
- fix length calculation
- improve error code setting
This commit is contained in:
Anatol Belski
2016-07-29 02:13:06 +02:00
parent 09b5a453c1
commit 41d172d20f
2 changed files with 12 additions and 12 deletions

View File

@@ -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;

View File

@@ -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;
}/*}}}*/