Merge topic 'file-LOCK-windows-message'

efaf987a cmFileLockResult: Avoid allocation for Windows error message

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !1035
This commit is contained in:
Brad King 2017-07-11 11:38:50 +00:00 committed by Kitware Robot
commit de72d5dda6

View File

@ -5,6 +5,7 @@
#include <errno.h>
#include <string.h>
#define WINMSG_BUF_LEN (1024)
cmFileLockResult cmFileLockResult::MakeOk()
{
return cmFileLockResult(OK, 0);
@ -53,18 +54,12 @@ std::string cmFileLockResult::GetOutputMessage() const
case SYSTEM:
#if defined(_WIN32)
{
char* errorText = NULL;
// http://stackoverflow.com/a/455533/2288008
DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS;
::FormatMessageA(flags, NULL, this->ErrorValue,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&errorText, 0, NULL);
if (errorText != NULL) {
const std::string message = errorText;
::LocalFree(errorText);
char winmsg[WINMSG_BUF_LEN];
DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
if (FormatMessageA(flags, NULL, this->ErrorValue,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)winmsg, WINMSG_BUF_LEN, NULL)) {
const std::string message = winmsg;
return message;
} else {
return "Internal error (FormatMessageA failed)";