Fix GetWindowsErrSz nullptr bug (#10282)

Summary:
`GetWindowsErrSz` may assign a `nullptr` to `std::string` in the event it cannot format the error code to a string. This will result in a crash when `std::string` attempts to calculate the length from `nullptr`.

The change here checks the output from `FormatMessageA` and only assigns to the otuput `std::string` if it is not null. Additionally, the call to free the buffer is only made if a non-null value is returned from `FormatMessageA`. In the event `FormatMessageA` does not output a string, an empty string is returned instead.

Fixes https://github.com/facebook/rocksdb/issues/10274

Pull Request resolved: https://github.com/facebook/rocksdb/pull/10282

Reviewed By: riversand963

Differential Revision: D37542143

Pulled By: ajkr

fbshipit-source-id: c21f5119ddb451f76960acec94639d0f538052f2
main
Johnny Shaw 3 years ago committed by Facebook GitHub Bot
parent 490fcac078
commit c2dc4c0c52
  1. 9
      port/win/io_win.cc

@ -37,15 +37,18 @@ inline bool IsAligned(size_t alignment, const void* ptr) {
} // namespace
std::string GetWindowsErrSz(DWORD err) {
LPSTR lpMsgBuf;
std::string Err;
LPSTR lpMsgBuf = nullptr;
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err,
0, // Default language
reinterpret_cast<LPSTR>(&lpMsgBuf), 0, NULL);
std::string Err = lpMsgBuf;
LocalFree(lpMsgBuf);
if (lpMsgBuf) {
Err = lpMsgBuf;
LocalFree(lpMsgBuf);
}
return Err;
}

Loading…
Cancel
Save