fixed off by 1 bug in getModulePath_ return value. Fixes #5

When passing -1 as input length, WideCharToMultiByte null-terminates
output which we do not want.
This commit is contained in:
Gregory Pakosz 2016-09-08 18:57:45 +02:00
parent 77ab6870d6
commit ba75d51713

View File

@ -66,7 +66,7 @@ static int WAI_PREFIX(getModulePath_)(HMODULE module, char* out, int capacity, i
for (;;) for (;;)
{ {
DWORD size; DWORD size;
int length_; int length_, length__;
size = GetModuleFileNameW(module, buffer1, sizeof(buffer1) / sizeof(buffer1[0])); size = GetModuleFileNameW(module, buffer1, sizeof(buffer1) / sizeof(buffer1[0]));
@ -96,18 +96,19 @@ static int WAI_PREFIX(getModulePath_)(HMODULE module, char* out, int capacity, i
if (!_wfullpath(buffer2, path, MAX_PATH)) if (!_wfullpath(buffer2, path, MAX_PATH))
break; break;
length_ = WideCharToMultiByte(CP_UTF8, 0, buffer2, -1, out, capacity, NULL, NULL); length_ = (int)wcslen(buffer2);
length__ = WideCharToMultiByte(CP_UTF8, 0, buffer2, length_ , out, capacity, NULL, NULL);
if (length_ == 0) if (length__ == 0)
length_ = WideCharToMultiByte(CP_UTF8, 0, buffer2, -1, NULL, 0, NULL, NULL); length__ = WideCharToMultiByte(CP_UTF8, 0, buffer2, length_, NULL, 0, NULL, NULL);
if (length_ == 0) if (length__ == 0)
break; break;
if (length_ <= capacity && dirname_length) if (length__ <= capacity && dirname_length)
{ {
int i; int i;
for (i = length_ - 1; i >= 0; --i) for (i = length__ - 1; i >= 0; --i)
{ {
if (out[i] == '\\') if (out[i] == '\\')
{ {
@ -117,7 +118,7 @@ static int WAI_PREFIX(getModulePath_)(HMODULE module, char* out, int capacity, i
} }
} }
length = length_; length = length__;
break; break;
} }