fs: realpath: fix string size before converting

fs__realpath_handle() removes the UNC prefixes \\?\ and \\?\UNC\
from paths returned by GetFinalPathNameByHandle, but doesn't
update the string length before sending it to fs__wide_to_utf8.
Fix that by decrement the correct size from the string length.

PR-URL: https://github.com/libuv/libuv/pull/733
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
This commit is contained in:
Yuval Brik 2016-02-20 17:42:39 +02:00 committed by Saúl Ibarra Corretgé
parent c7c8e916b8
commit 5dc15cc269

View File

@ -1717,25 +1717,26 @@ static void fs__readlink(uv_fs_t* req) {
static size_t fs__realpath_handle(HANDLE handle, char** realpath_ptr) {
int r;
DWORD w_realpath_len;
WCHAR* w_realpath_ptr;
WCHAR* w_finalpath_ptr = NULL;
WCHAR* w_realpath_ptr = NULL;
WCHAR* w_realpath_buf;
w_realpath_len = pGetFinalPathNameByHandleW(handle, NULL, 0, VOLUME_NAME_DOS);
if (w_realpath_len == 0) {
return -1;
}
w_realpath_ptr = uv__malloc((w_realpath_len + 1) * sizeof(WCHAR));
if (w_realpath_ptr == NULL) {
w_realpath_buf = uv__malloc((w_realpath_len + 1) * sizeof(WCHAR));
if (w_realpath_buf == NULL) {
SetLastError(ERROR_OUTOFMEMORY);
return -1;
}
w_realpath_ptr = w_realpath_buf;
if (pGetFinalPathNameByHandleW(handle,
w_realpath_ptr,
w_realpath_len,
VOLUME_NAME_DOS) == 0) {
uv__free(w_realpath_ptr);
uv__free(w_realpath_buf);
SetLastError(ERROR_INVALID_HANDLE);
return -1;
}
@ -1744,20 +1745,22 @@ static size_t fs__realpath_handle(HANDLE handle, char** realpath_ptr) {
if (wcsncmp(w_realpath_ptr,
UNC_PATH_PREFIX,
UNC_PATH_PREFIX_LEN) == 0) {
w_finalpath_ptr = w_realpath_ptr + 6;
*w_finalpath_ptr = L'\\';
w_realpath_ptr += 6;
*w_realpath_ptr = L'\\';
w_realpath_len -= 6;
} else if (wcsncmp(w_realpath_ptr,
LONG_PATH_PREFIX,
LONG_PATH_PREFIX_LEN) == 0) {
w_finalpath_ptr = w_realpath_ptr + 4;
w_realpath_ptr += 4;
w_realpath_len -= 4;
} else {
uv__free(w_realpath_ptr);
uv__free(w_realpath_buf);
SetLastError(ERROR_INVALID_HANDLE);
return -1;
}
r = fs__wide_to_utf8(w_finalpath_ptr, w_realpath_len, realpath_ptr, NULL);
uv__free(w_realpath_ptr);
r = fs__wide_to_utf8(w_realpath_ptr, w_realpath_len, realpath_ptr, NULL);
uv__free(w_realpath_buf);
return r;
}