Fix `GetFullPathNameW`, `GetEnvironmentVariableW` (#43)

This commit is contained in:
Luke Street 2023-09-11 05:36:13 -04:00 committed by GitHub
parent b7e8e5fb80
commit d27fc944bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 22 deletions

View File

@ -34,15 +34,15 @@ typedef struct _EXCEPTION_POINTERS {
typedef LONG (*PVECTORED_EXCEPTION_HANDLER)(PEXCEPTION_POINTERS ExceptionInfo); typedef LONG (*PVECTORED_EXCEPTION_HANDLER)(PEXCEPTION_POINTERS ExceptionInfo);
namespace kernel32 { namespace kernel32 {
static int wstrlen(const uint16_t *str) { static size_t wstrlen(const uint16_t *str) {
int len = 0; size_t len = 0;
while (str[len] != 0) while (str[len] != 0)
++len; ++len;
return len; return len;
} }
static int wstrncpy(uint16_t *dst, const uint16_t *src, int n) { static size_t wstrncpy(uint16_t *dst, const uint16_t *src, size_t n) {
int i = 0; size_t i = 0;
while (i < n && src[i] != 0) { while (i < n && src[i] != 0) {
dst[i] = src[i]; dst[i] = src[i];
++i; ++i;
@ -538,17 +538,15 @@ namespace kernel32 {
const auto absStrW = stringToWideString(absStr.c_str()); const auto absStrW = stringToWideString(absStr.c_str());
DEBUG_LOG("-> %s\n", absStr.c_str()); DEBUG_LOG("-> %s\n", absStr.c_str());
const DWORD absStrWLen = wstrlen(absStrW); const auto len = wstrlen(absStrW);
const DWORD absStrWSize = absStrWLen * 2; if (nBufferLength < len + 1) {
if ((absStrWSize + 2) <= nBufferLength) {
wstrncpy(lpBuffer, absStrW, (int)absStrWLen);
assert(!lpFilePart);
free(absStrW); free(absStrW);
return absStrWSize; return len + 1;
} else {
free(absStrW);
return absStrWSize + 2;
} }
wstrncpy(lpBuffer, absStrW, len + 1);
assert(!lpFilePart);
free(absStrW);
return len;
} }
/** /**
@ -1792,16 +1790,14 @@ namespace kernel32 {
if (!value) { if (!value) {
return 0; return 0;
} }
unsigned int len = strlen(value) * 2; uint16_t *wideValue = stringToWideString(value);
if (nSize == 0) { const auto len = wstrlen(wideValue);
return len + 2; if (nSize < len + 1) {
free(wideValue);
return len + 1;
} }
if (nSize < len) { wstrncpy(lpBuffer, wideValue, len + 1);
return len; free(wideValue);
}
const uint16_t *wideValue = stringToWideString(value);
memcpy(lpBuffer, wideValue, len + 2);
free((void *)wideValue);
return len; return len;
} }