Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/backend/common/module_loading_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,26 @@ LibHandle loadLibrary(const char* library_name) {
void unloadLibrary(LibHandle handle) { FreeLibrary(handle); }

string getErrorMessage() {
const char* lpMsgBuf;
DWORD dw = GetLastError();
char* lpMsgBuf = nullptr;
DWORD dw = GetLastError();

// FormatMessage only writes lpMsgBuf when it succeeds. On failure it
// returns 0 and leaves the pointer untouched, so it must be initialized
// and the result checked before it is dereferenced.
DWORD chars = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuf), 0, NULL);

if (chars == 0 || lpMsgBuf == nullptr) {
return "Unknown error (system error code " + std::to_string(dw) + ")";
}

FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf, 0, NULL);
string error_message(lpMsgBuf);
// FORMAT_MESSAGE_ALLOCATE_BUFFER allocates with LocalAlloc; the caller
// owns the buffer and must release it.
LocalFree(lpMsgBuf);
return error_message;
}

Expand Down
Loading