[Openvpn-devel] Format Windows error message in Unicode

Message ID 20230418134011.1752777-1-selva.nair@gmail.com
State Superseded
Headers show
Series [Openvpn-devel] Format Windows error message in Unicode | expand

Commit Message

Selva Nair April 18, 2023, 1:40 p.m. UTC
From: Selva Nair <selva.nair@gmail.com>

- 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

Signed-off-by: Selva Nair <selva.nair@gmail.com>
---
 src/openvpn/error.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

Comments

Frank Lichtenheld April 18, 2023, 1:57 p.m. UTC | #1
On Tue, Apr 18, 2023 at 09:40:11AM -0400, selva.nair@gmail.com wrote:
> From: Selva Nair <selva.nair@gmail.com>
> 
> - 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
> 
> Signed-off-by: Selva Nair <selva.nair@gmail.com>
> ---
>  src/openvpn/error.c | 15 ++++++++++-----
>  1 file changed, 10 insertions(+), 5 deletions(-)
> 
> diff --git a/src/openvpn/error.c b/src/openvpn/error.c
> index a2c9aa4c..ef821fcd 100644
> --- a/src/openvpn/error.c
> +++ b/src/openvpn/error.c
> @@ -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.

>              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]");
>          }
> -- 
> 2.34.1
> 
> 
> 
> _______________________________________________
> Openvpn-devel mailing list
> Openvpn-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Patch

diff --git a/src/openvpn/error.c b/src/openvpn/error.c
index a2c9aa4c..ef821fcd 100644
--- a/src/openvpn/error.c
+++ b/src/openvpn/error.c
@@ -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]");
         }