From: Simon Rozman Date: Mon, 22 Mar 2021 10:39:57 +0000 (+0100) Subject: openvpnserv: Cache last error before it is overridden X-Git-Tag: v2.6_beta1~592 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1e938c50930f29124909e120a29fb116d4c46576;p=thirdparty%2Fopenvpn.git openvpnserv: Cache last error before it is overridden FormatMessage() sets the last error according to its own success. This looses the original error code leading to mismatched error message and error number when sprintfted together resulting in confusing event log message. Signed-off-by: Simon Rozman Acked-by: Gert Doering Message-Id: <20210322103957.1234-1-simon@rozman.si> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg21789.html Signed-off-by: Gert Doering --- diff --git a/src/openvpnserv/common.c b/src/openvpnserv/common.c index 48769be48..ebd086770 100644 --- a/src/openvpnserv/common.c +++ b/src/openvpnserv/common.c @@ -228,12 +228,14 @@ out: LPCTSTR GetLastErrorText() { + DWORD error; static TCHAR buf[256]; DWORD len; LPTSTR tmp = NULL; + error = GetLastError(); len = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY, - NULL, GetLastError(), LANG_NEUTRAL, (LPTSTR)&tmp, 0, NULL); + NULL, error, LANG_NEUTRAL, (LPTSTR)&tmp, 0, NULL); if (len == 0 || (long) _countof(buf) < (long) len + 14) { @@ -242,7 +244,7 @@ GetLastErrorText() else { tmp[_tcslen(tmp) - 2] = TEXT('\0'); /* remove CR/LF characters */ - openvpn_sntprintf(buf, _countof(buf), TEXT("%s (0x%x)"), tmp, GetLastError()); + openvpn_sntprintf(buf, _countof(buf), TEXT("%s (0x%x)"), tmp, error); } if (tmp)