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
This commit is contained in:
Johnny Shaw 2022-06-29 20:41:54 -07:00 committed by Facebook GitHub Bot
parent 490fcac078
commit c2dc4c0c52
1 changed files with 6 additions and 3 deletions

View File

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