Skip to content

Commit 0850d88

Browse files
committed
fixed problems with nSize (thanks to kao!)
1 parent 7e6fb23 commit 0850d88

File tree

2 files changed

+46
-47
lines changed

2 files changed

+46
-47
lines changed

DeviceNameResolver.cpp

+40-41
Original file line numberDiff line numberDiff line change
@@ -3,115 +3,114 @@
33
#include "DynBuf.h"
44
#include "NativeWinApi.h"
55

6-
extern "C" __declspec(dllexport) bool DevicePathToPathW(const wchar_t* szDevicePath, wchar_t* szPath, size_t nSize)
6+
extern "C" __declspec(dllexport) bool DevicePathToPathW(const wchar_t* szDevicePath, wchar_t* szPath, size_t nSizeInChars)
77
{
88
DeviceNameResolver deviceNameResolver;
99
wchar_t targetPath[MAX_PATH] = L"";
1010
if(!deviceNameResolver.resolveDeviceLongNameToShort(szDevicePath, targetPath))
1111
return false;
12-
wcscpy_s(szPath, nSize / sizeof(wchar_t), targetPath);
12+
wcsncpy_s(szPath, nSizeInChars, targetPath, _TRUNCATE);
1313
return true;
1414
}
1515

16-
extern "C" __declspec(dllexport) bool DevicePathToPathA(const char* szDevicePath, char* szPath, size_t nSize)
16+
extern "C" __declspec(dllexport) bool DevicePathToPathA(const char* szDevicePath, char* szPath, size_t nSizeInChars)
1717
{
1818
size_t len = strlen(szDevicePath);
19-
DynBuf newDevicePathBuf((len + 1)*sizeof(wchar_t));
19+
DynBuf newDevicePathBuf((len + 1) * sizeof(wchar_t));
2020
wchar_t* newDevicePath = (wchar_t*)newDevicePathBuf.GetPtr();
2121
*newDevicePath = L'\0';
22-
if(MultiByteToWideChar(CP_ACP, NULL, szDevicePath, -1, newDevicePath, (int)len + 1))
22+
if(MultiByteToWideChar(CP_ACP, NULL, szDevicePath, -1, newDevicePath, int(len + 1)))
2323
{
24-
DynBuf newPathBuf(nSize * sizeof(wchar_t));
24+
DynBuf newPathBuf((nSizeInChars + 1) * sizeof(wchar_t));
2525
wchar_t* newPath = (wchar_t*)newPathBuf.GetPtr();
26-
if(!DevicePathToPathW(newDevicePath, newPath, nSize * sizeof(wchar_t)))
26+
if(!DevicePathToPathW(newDevicePath, newPath, nSizeInChars))
2727
return false;
28-
if(!WideCharToMultiByte(CP_ACP, NULL, newPath, -1, szPath, (int)wcslen(newPath) + 1, NULL, NULL))
28+
if(!WideCharToMultiByte(CP_ACP, NULL, newPath, -1, szPath, int(wcslen(newPath)) + 1, NULL, NULL))
2929
return false;
3030
}
3131
return true;
3232
}
3333

34-
__declspec(dllexport) bool DevicePathFromFileHandleW(HANDLE hFile, wchar_t* szDevicePath, size_t nSize)
34+
__declspec(dllexport) bool DevicePathFromFileHandleW(HANDLE hFile, wchar_t* szDevicePath, size_t nSizeInChars)
3535
{
3636
NativeWinApi::initialize();
3737
ULONG ReturnLength;
3838
bool bRet = false;
3939
if(NativeWinApi::NtQueryObject(hFile, ObjectNameInformation, 0, 0, &ReturnLength) == STATUS_INFO_LENGTH_MISMATCH)
4040
{
4141
ReturnLength += 0x2000; //on Windows XP SP3 ReturnLength will not be set just add some buffer space to fix this
42-
POBJECT_NAME_INFORMATION NameInformation = (POBJECT_NAME_INFORMATION)GlobalAlloc(0, ReturnLength);
42+
POBJECT_NAME_INFORMATION NameInformation = POBJECT_NAME_INFORMATION(GlobalAlloc(0, ReturnLength));
4343
if(NativeWinApi::NtQueryObject(hFile, ObjectNameInformation, NameInformation, ReturnLength, 0) == STATUS_SUCCESS)
4444
{
4545
NameInformation->Name.Buffer[NameInformation->Name.Length / 2] = L'\0'; //null-terminate the UNICODE_STRING
46-
if(wcslen(NameInformation->Name.Buffer) < nSize)
47-
{
48-
wcscpy_s(szDevicePath, nSize / sizeof(wchar_t), NameInformation->Name.Buffer);
49-
bRet = true;
50-
}
46+
wcsncpy_s(szDevicePath, nSizeInChars, NameInformation->Name.Buffer, _TRUNCATE);
47+
bRet = true;
5148
}
5249
GlobalFree(NameInformation);
5350
}
51+
if(!bRet)
52+
return false;
5453
if(_wcsnicmp(szDevicePath, L"\\Device\\LanmanRedirector\\", 25) == 0) // Win XP
5554
{
56-
wcscpy_s(szDevicePath, nSize / sizeof(wchar_t), L"\\\\");
57-
wcscat_s(szDevicePath, nSize / sizeof(wchar_t), &szDevicePath[25]);
55+
wcsncpy_s(szDevicePath, nSizeInChars, L"\\\\", _TRUNCATE);
56+
wcsncat_s(szDevicePath, nSizeInChars, &szDevicePath[25], _TRUNCATE);
5857
}
5958
else if(_wcsnicmp(szDevicePath, L"\\Device\\Mup\\", 12) == 0) // Win 7
6059
{
61-
wcscpy_s(szDevicePath, nSize / sizeof(wchar_t), L"\\\\");
62-
wcscat_s(szDevicePath, nSize / sizeof(wchar_t), &szDevicePath[12]);
60+
wcsncpy_s(szDevicePath, nSizeInChars, L"\\\\", _TRUNCATE);
61+
wcsncat_s(szDevicePath, nSizeInChars, &szDevicePath[12], _TRUNCATE);
6362
}
64-
return bRet;
63+
return true;
6564
}
6665

67-
__declspec(dllexport) bool DevicePathFromFileHandleA(HANDLE hFile, char* szDevicePath, size_t nSize)
66+
__declspec(dllexport) bool DevicePathFromFileHandleA(HANDLE hFile, char* szDevicePath, size_t nSizeInChars)
6867
{
69-
DynBuf newDevicePathBuf(nSize * sizeof(wchar_t));
68+
DynBuf newDevicePathBuf((nSizeInChars + 1) * sizeof(wchar_t));
7069
wchar_t* newDevicePath = (wchar_t*)newDevicePathBuf.GetPtr();
71-
if(!DevicePathFromFileHandleW(hFile, newDevicePath, nSize * sizeof(wchar_t)))
70+
if(!DevicePathFromFileHandleW(hFile, newDevicePath, nSizeInChars))
7271
return false;
73-
if(!WideCharToMultiByte(CP_ACP, NULL, newDevicePath, -1, szDevicePath, (int)wcslen(newDevicePath) + 1, NULL, NULL))
72+
if(!WideCharToMultiByte(CP_ACP, NULL, newDevicePath, -1, szDevicePath, int(wcslen(newDevicePath)) + 1, NULL, NULL))
7473
return false;
7574
return true;
7675
}
7776

78-
__declspec(dllexport) bool PathFromFileHandleW(HANDLE hFile, wchar_t* szPath, size_t nSize)
77+
__declspec(dllexport) bool PathFromFileHandleW(HANDLE hFile, wchar_t* szPath, size_t nSizeInChars)
7978
{
8079
typedef DWORD (WINAPI * GETFINALPATHNAMEBYHANDLEW)(
81-
IN HANDLE hFile,
82-
OUT wchar_t* lpszFilePath,
83-
IN DWORD cchFilePath,
84-
IN DWORD dwFlags
80+
IN HANDLE /*hFile*/,
81+
OUT wchar_t* /*lpszFilePath*/,
82+
IN DWORD /*cchFilePath*/,
83+
IN DWORD /*dwFlags*/
8584
);
86-
static GETFINALPATHNAMEBYHANDLEW GetFPNBHW = (GETFINALPATHNAMEBYHANDLEW)GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "GetFinalPathNameByHandleW");
87-
if(GetFPNBHW && GetFPNBHW(hFile, szPath, (DWORD)(nSize / sizeof(wchar_t)), 0))
85+
static GETFINALPATHNAMEBYHANDLEW GetFPNBHW = GETFINALPATHNAMEBYHANDLEW(GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "GetFinalPathNameByHandleW"));
86+
if(GetFPNBHW && GetFPNBHW(hFile, szPath, DWORD(nSizeInChars), 0))
8887
{
8988
if(_wcsnicmp(szPath, L"\\\\?\\UNC\\", 8) == 0) // Server path
9089
{
91-
wcscpy_s(szPath, nSize / sizeof(wchar_t), L"\\\\");
92-
wcscat_s(szPath, nSize / sizeof(wchar_t), &szPath[8]);
90+
wcsncpy_s(szPath, nSizeInChars, L"\\\\", _TRUNCATE);
91+
wcsncat_s(szPath, nSizeInChars, &szPath[8], _TRUNCATE);
9392
}
9493
else if(_wcsnicmp(szPath, L"\\\\?\\", 4) == 0 && szPath[5] == L':') // Drive path
9594
{
96-
wcscpy_s(szPath, nSize / sizeof(wchar_t), &szPath[4]);
95+
wcsncpy_s(szPath, nSizeInChars, &szPath[4], _TRUNCATE);
9796
}
9897
return true;
9998
}
100-
if(!DevicePathFromFileHandleW(hFile, szPath, nSize))
99+
if(!DevicePathFromFileHandleW(hFile, szPath, nSizeInChars))
101100
return false;
102101
std::wstring oldPath(szPath);
103-
if(!DevicePathToPathW(szPath, szPath, nSize))
104-
wcscpy_s(szPath, nSize / sizeof(wchar_t), oldPath.c_str());
102+
if(!DevicePathToPathW(szPath, szPath, nSizeInChars))
103+
wcsncpy_s(szPath, nSizeInChars, oldPath.c_str(), _TRUNCATE);
105104
return true;
106105
}
107106

108-
__declspec(dllexport) bool PathFromFileHandleA(HANDLE hFile, char* szPath, size_t nSize)
107+
__declspec(dllexport) bool PathFromFileHandleA(HANDLE hFile, char* szPath, size_t nSizeInChars)
109108
{
110-
DynBuf newDevicePathBuf(nSize * sizeof(wchar_t));
109+
DynBuf newDevicePathBuf((nSizeInChars + 1) * sizeof(wchar_t));
111110
wchar_t* newDevicePath = (wchar_t*)newDevicePathBuf.GetPtr();
112-
if(!PathFromFileHandleW(hFile, newDevicePath, nSize * sizeof(wchar_t)))
111+
if(!PathFromFileHandleW(hFile, newDevicePath, nSizeInChars))
113112
return false;
114-
if(!WideCharToMultiByte(CP_ACP, NULL, newDevicePath, -1, szPath, (int)wcslen(newDevicePath) + 1, NULL, NULL))
113+
if(!WideCharToMultiByte(CP_ACP, NULL, newDevicePath, -1, szPath, int(wcslen(newDevicePath)) + 1, NULL, NULL))
115114
return false;
116115
return true;
117116
}

DeviceNameResolver.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ extern "C"
88
{
99
#endif
1010

11-
__declspec(dllexport) bool DevicePathToPathW(const wchar_t* szDevicePath, wchar_t* szPath, size_t nSize);
12-
__declspec(dllexport) bool DevicePathToPathA(const char* szDevicePath, char* szPath, size_t nSize);
13-
__declspec(dllexport) bool DevicePathFromFileHandleW(HANDLE hFile, wchar_t* szDevicePath, size_t nSize);
14-
__declspec(dllexport) bool DevicePathFromFileHandleA(HANDLE hFile, char* szDevicePath, size_t nSize);
15-
__declspec(dllexport) bool PathFromFileHandleW(HANDLE hFile, wchar_t* szPath, size_t nSize);
16-
__declspec(dllexport) bool PathFromFileHandleA(HANDLE hFile, char* szPath, size_t nSize);
11+
__declspec(dllexport) bool DevicePathToPathW(const wchar_t* szDevicePath, wchar_t* szPath, size_t nSizeInChars);
12+
__declspec(dllexport) bool DevicePathToPathA(const char* szDevicePath, char* szPath, size_t nSizeInChars);
13+
__declspec(dllexport) bool DevicePathFromFileHandleW(HANDLE hFile, wchar_t* szDevicePath, size_t nSizeInChars);
14+
__declspec(dllexport) bool DevicePathFromFileHandleA(HANDLE hFile, char* szDevicePath, size_t nSizeInChars);
15+
__declspec(dllexport) bool PathFromFileHandleW(HANDLE hFile, wchar_t* szPath, size_t nSizeInChars);
16+
__declspec(dllexport) bool PathFromFileHandleA(HANDLE hFile, char* szPath, size_t nSizeInChars);
1717

1818
#ifdef __cplusplus
1919
}

0 commit comments

Comments
 (0)