From 2692ba26977bc2c981e083b05870ae193f83f813 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Wed, 16 Mar 2011 21:08:20 +0000 Subject: [PATCH] Make "GetLastErrorMsg" thread safe. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@7359 8ced0084-cf51-0410-be5f-012b33b47a6e --- Source/Core/Common/Src/Misc.cpp | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Source/Core/Common/Src/Misc.cpp b/Source/Core/Common/Src/Misc.cpp index baa8266ced..3b075f3dbc 100644 --- a/Source/Core/Common/Src/Misc.cpp +++ b/Source/Core/Common/Src/Misc.cpp @@ -20,19 +20,22 @@ // Generic function to get last error message. // Call directly after the command or use the error num. // This function might change the error code. -const char *GetLastErrorMsg() +const char* GetLastErrorMsg() { - // FIXME : not thread safe. - // Caused by sloppy use in logging in FileUtil.cpp, primarily. What to do, what to do ... - static char errStr[255] = {0}; + static const size_t buff_size = 255; + #ifdef _WIN32 - DWORD dw = GetLastError(); - FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dw, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPTSTR) errStr, 254, NULL ); + static __declspec(thread) char err_str[buff_size] = {}; + + FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + err_str, buff_size, NULL); #else + static __thread char err_str[buff_size] = {}; + // Thread safe (XSI-compliant) - strerror_r(errno, errStr, 255); + strerror_r(errno, err_str, buff_size); #endif - return errStr; + + return err_str; }