Skip to contentSkip to conversation

openvpnserv: Cache last error before it is overridden

PATCH In review Simon Rozman 2 participants 2 messages Mar 22, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

---
 src/openvpnserv/common.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
src/openvpnserv/common.c +4 -2
index 48769be4..ebd08677 100644
@@ -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)
Signature · 1 line
2.30.0.windows.2
Acked-by on v1 1/1
#1 lore raw

Explanation and code make sense. Test compiled on Ubuntu18/MinGW.

Your patch has been applied to the master and release/2.5 branch (bugfix).

And I managed to unmess the From: line before pushing :-) (and I *do* have a pre-push-hook in place now that wakes me up next monday morning)

commit 1e938c50930f29124909e120a29fb116d4c46576 (master) commit 24c1fc2a0ab5fa113d6769ab090de12e3719ab6b (release/2.5)

Author: Simon Rozman
Date:   Mon Mar 22 11:39:57 2021 +0100
     openvpnserv: Cache last error before it is overridden

     Signed-off-by: Simon Rozman <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg21789.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering