Skip to contentSkip to conversation

Format Windows error message in Unicode

PATCH Superseded [email protected]... 3 participants 5 messages Apr 18, 2023 – May 4, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Format Windows error message in Unicode

From: Selva Nair <[email protected]...>

- We assume that all text passed to the management interface and written to log file are in Unicode (UTF-8). This is broken by the use of the ANSI version of FormatMessage() for Windows error messages. Fix by using FormatMessageW() and converting the UTF-16 result to UTF-8.

Github: fixes OpenVPN/openvpn#319

---
 src/openvpn/error.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
src/openvpn/error.c +10 -5
index a2c9aa4c..ef821fcd 100644
@@ -970,19 +970,24 @@ strerror_win32(DWORD errnum, struct gc_arena *gc)
 
     /* format a windows error message */
     {
-        char message[256];
+        wchar_t wmessage[256];
+        char *message = NULL;
         struct buffer out = alloc_buf_gc(256, gc);
-        const int status =  FormatMessage(
+        const int status =  FormatMessageW(
             FORMAT_MESSAGE_IGNORE_INSERTS
             | FORMAT_MESSAGE_FROM_SYSTEM
             | FORMAT_MESSAGE_ARGUMENT_ARRAY,
             NULL,
             errnum,
             0,
-            message,
-            sizeof(message),
+            wmessage,
+            SIZE(wmessage),
             NULL);
-        if (!status)
+        if (status)
+        {
+            message = utf16to8(wmessage, gc);
+        }
+        if (!status || !message)
         {
             buf_printf(&out, "[Unknown Win32 Error]");
         }
Signature · 1 line
2.34.1
#1 lore raw
Quoted 20 lines · On Tue, Apr 18, 2023 at 09:40:11AM -0400, [email protected]... wrote:

From: Selva Nair <[email protected]...>

- We assume that all text passed to the management interface and written to log file are in Unicode (UTF-8). This is broken by the use of the ANSI version of FormatMessage() for Windows error messages. Fix by using FormatMessageW() and converting the UTF-16 result to UTF-8.

Github: fixes OpenVPN/openvpn#319

---
 src/openvpn/error.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
src/openvpn/error.c +0 -0
index a2c9aa4c..ef821fcd 100644

src/openvpn/error.c strerror_win32(DWORD errnum, struct gc_arena *gc) · line 976

Quoted 9 lines
src/openvpn/error.c +3 -2
@@ -970,19 +970,24 @@ strerror_win32(DWORD errnum, struct gc_arena *gc)
 
     /* format a windows error message */
     {
-        char message[256];
+        wchar_t wmessage[256];
+        char *message = NULL;
         struct buffer out = alloc_buf_gc(256, gc);
-        const int status =  FormatMessage(
+        const int status =  FormatMessageW(

According to the documentation the function returns DWORD, not int. When we touch the code anyway, we should clean that up.

Quoted 29 lines
src/openvpn/error.c +7 -3
             FORMAT_MESSAGE_IGNORE_INSERTS
             | FORMAT_MESSAGE_FROM_SYSTEM
             | FORMAT_MESSAGE_ARGUMENT_ARRAY,
             NULL,
             errnum,
             0,
-            message,
-            sizeof(message),
+            wmessage,
+            SIZE(wmessage),
             NULL);
-        if (!status)
+        if (status)
+        {
+            message = utf16to8(wmessage, gc);
+        }
+        if (!status || !message)
         {
             buf_printf(&out, "[Unknown Win32 Error]");
         }
Signature · 8 lines
2.34.1



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Signature · 1 line
  Frank Lichtenheld
#2 lore raw

PATCH v2 1/1 Format Windows error message in Unicode

In reply to [email protected]...

From: Selva Nair <[email protected]...>

- We assume that all text passed to the management interface and written to log file are in Unicode (UTF-8). This is broken by the use of the ANSI version of FormatMessage() for Windows error messages. Fix by using FormatMessageW() and converting the UTF-16 result to UTF-8.

v2: assign return value of FormatMessageW() to DWORD, not int

Github: fixes OpenVPN/openvpn#319

---
 src/openvpn/error.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
src/openvpn/error.c +10 -5
index a2c9aa4c..9a234e67 100644
@@ -970,19 +970,24 @@ strerror_win32(DWORD errnum, struct gc_arena *gc)
 
     /* format a windows error message */
     {
-        char message[256];
+        wchar_t wmessage[256];
+        char *message = NULL;
         struct buffer out = alloc_buf_gc(256, gc);
-        const int status =  FormatMessage(
+        const DWORD status =  FormatMessageW(
             FORMAT_MESSAGE_IGNORE_INSERTS
             | FORMAT_MESSAGE_FROM_SYSTEM
             | FORMAT_MESSAGE_ARGUMENT_ARRAY,
             NULL,
             errnum,
             0,
-            message,
-            sizeof(message),
+            wmessage,
+            SIZE(wmessage),
             NULL);
-        if (!status)
+        if (status)
+        {
+            message = utf16to8(wmessage, gc);
+        }
+        if (!status || !message)
         {
             buf_printf(&out, "[Unknown Win32 Error]");
         }
Signature · 1 line
2.34.1
Acked-by on v2 1/1
#3 lore raw
Quoted 8 lines · On Tue, Apr 18, 2023 at 10:14:46AM -0400, [email protected]... wrote:

From: Selva Nair <[email protected]...>

- We assume that all text passed to the management interface and written to log file are in Unicode (UTF-8). This is broken by the use of the ANSI version of FormatMessage() for Windows error messages. Fix by using FormatMessageW() and converting the UTF-16 result to UTF-8.

Quoted 8 lines

v2: assign return value of FormatMessageW() to DWORD, not int

Github: fixes OpenVPN/openvpn#319

---
 src/openvpn/error.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

Looks good to me.

Regards,

Signature · 1 line
  Frank Lichtenheld
#4 lore raw
In reply to [email protected]...

I have not tested this myself (as in "trigger a nationalized error message with non-ASCII characters in it"), but have test-built on mingw + GHA.

Your patch has been applied to the master and release/2.6 branch.

commit fed67642dccbcf115952df0709a98929c1fc52b8 (master) commit d8cb1b5a397edf287745372afe9a6457edaf2ae8 (release/2.6)

Author: Selva Nair
Date:   Tue Apr 18 10:14:46 2023 -0400
     Format Windows error message in Unicode

     Signed-off-by: Selva Nair <[email protected]...>
     Acked-by: Frank Lichtenheld <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg26598.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering