From ad63e2593db278f34fc0e1ee6bbae926c62d8caa Mon Sep 17 00:00:00 2001 From: Ori Levari Date: Thu, 30 Apr 2020 20:51:09 -0700 Subject: [PATCH] avoid using LocalFree on FormatMessageA buffer (#3772) * avoid using localfree for FormatMessageA buffer because it is only supported on windows 10 Co-authored-by: Ori Levari --- onnxruntime/core/platform/path_lib.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/onnxruntime/core/platform/path_lib.h b/onnxruntime/core/platform/path_lib.h index 067f473afa..30dc7ffae8 100644 --- a/onnxruntime/core/platform/path_lib.h +++ b/onnxruntime/core/platform/path_lib.h @@ -178,11 +178,10 @@ inline OrtFileType DTToFileType(DWORD dwFileAttributes) { return OrtFileType::TYPE_REG; } inline std::string FormatErrorCode(DWORD dw) { - char* lpMsgBuf; - FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&lpMsgBuf, 0, NULL); - std::string s(lpMsgBuf); - LocalFree(lpMsgBuf); + static const DWORD bufferLength = 64 * 1024; + std::string s(bufferLength, '\0'); + FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)s.data(), bufferLength / sizeof(TCHAR), NULL); return s; } template