diff options
author | David Majnemer <david.majnemer@gmail.com> | 2013-10-07 09:52:36 +0000 |
---|---|---|
committer | David Majnemer <david.majnemer@gmail.com> | 2013-10-07 09:52:36 +0000 |
commit | ae06a63be5a1279739e0c8a2006e72f4bc687d57 (patch) | |
tree | 636622c31be132769d0c843fb4cd9ce516e8399e /lib/Support/Windows/Path.inc | |
parent | c84c742eddc0c57c34271471f332c9857d79e672 (diff) | |
download | external_llvm-ae06a63be5a1279739e0c8a2006e72f4bc687d57.zip external_llvm-ae06a63be5a1279739e0c8a2006e72f4bc687d57.tar.gz external_llvm-ae06a63be5a1279739e0c8a2006e72f4bc687d57.tar.bz2 |
Windows: Be more explicit with Win32 APIs
This addresses several issues in a similar vein:
- Use the Unicode APIs when possible, running nm on clang shows that we
only use Unicode APIs except for FormatMessage, CreateSemaphore, and
GetModuleHandle. AFAICT, the latter two are coming from MinGW and
not LLVM itself.
- Make getMainExecutable more resilient. It previously considered
return values of zero from ::GetModuleFileNameA to be acceptable.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@192096 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support/Windows/Path.inc')
-rw-r--r-- | lib/Support/Windows/Path.inc | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/lib/Support/Windows/Path.inc b/lib/Support/Windows/Path.inc index 998ec42..0b39198 100644 --- a/lib/Support/Windows/Path.inc +++ b/lib/Support/Windows/Path.inc @@ -46,9 +46,9 @@ namespace { /*__in*/ LPCWSTR lpTargetFileName, /*__in*/ DWORD dwFlags); - PtrCreateSymbolicLinkW create_symbolic_link_api = PtrCreateSymbolicLinkW( - ::GetProcAddress(::GetModuleHandleA("kernel32.dll"), - "CreateSymbolicLinkW")); + PtrCreateSymbolicLinkW create_symbolic_link_api = + PtrCreateSymbolicLinkW(::GetProcAddress( + ::GetModuleHandleW(L"Kernel32.dll"), "CreateSymbolicLinkW")); error_code TempDir(SmallVectorImpl<wchar_t> &result) { retry_temp_dir: @@ -216,9 +216,28 @@ namespace sys { namespace fs { std::string getMainExecutable(const char *argv0, void *MainExecAddr) { - char pathname[MAX_PATH]; - DWORD ret = ::GetModuleFileNameA(NULL, pathname, MAX_PATH); - return ret != MAX_PATH ? pathname : ""; + SmallVector<wchar_t, MAX_PATH> PathName; + DWORD Size = ::GetModuleFileNameW(NULL, PathName.data(), PathName.capacity()); + + // A zero return value indicates a failure other than insufficient space. + if (Size == 0) + return ""; + + // Insufficient space is determined by a return value equal to the size of + // the buffer passed in. + if (Size == PathName.capacity()) + return ""; + + // On success, GetModuleFileNameW returns the number of characters written to + // the buffer not including the NULL terminator. + PathName.set_size(Size); + + // Convert the result from UTF-16 to UTF-8. + SmallVector<char, MAX_PATH> PathNameUTF8; + if (UTF16ToUTF8(PathName.data(), PathName.size(), PathNameUTF8)) + return ""; + + return std::string(PathNameUTF8.data()); } UniqueID file_status::getUniqueID() const { @@ -672,12 +691,11 @@ error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset) { case priv: flprotect = PAGE_WRITECOPY; break; } - FileMappingHandle = ::CreateFileMapping(FileHandle, - 0, - flprotect, - (Offset + Size) >> 32, - (Offset + Size) & 0xffffffff, - 0); + FileMappingHandle = + ::CreateFileMappingW(FileHandle, 0, flprotect, + (Offset + Size) >> 32, + (Offset + Size) & 0xffffffff, + 0); if (FileMappingHandle == NULL) { error_code ec = windows_error(GetLastError()); if (FileDescriptor) { |