[Openvpn-devel,v2] service: add utf8to16 function that takes a size

Message ID 20241221224136.20984-1-gert@greenie.muc.de
State Accepted
Headers show
Series [Openvpn-devel,v2] service: add utf8to16 function that takes a size | expand

Commit Message

Gert Doering Dec. 21, 2024, 10:41 p.m. UTC
From: Heiko Hund <heiko@ist.eigentlich.net>

utf8to16_size() takes the size of the to be converted string. This is
needed to convert MULTI_SZ strings, which contain inline NUL characters,
but can be useful in other cases as well.

Change-Id: I6b4aa3d63c0b684bf95841271c04bc5d9c37793b
Signed-off-by: Heiko Hund <heiko@ist.eigentlich.net>
Acked-by: Gert Doering <gert@greenie.muc.de>
---

This change was reviewed on Gerrit and approved by at least one
developer. I request to merge it to master.

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/836
This mail reflects revision 2 of this Change.

Acked-by according to Gerrit (reflected above):
Gert Doering <gert@greenie.muc.de>

Comments

Gert Doering Dec. 22, 2024, 10:47 a.m. UTC | #1
v1 looked like a nice and trivial change with not much to go wrong, and
then GHA exploded - one of the builds runs without "-O2", and with
an inline function without "static inline" this leads to link time
errors (function is not inlined due to missing -O2, but not instantiated
either, so "nowhere to be found").  v1 to v2 only differ in the "static",
so I'm recording Frank's ACK on v1 plus adding my own.

Your patch has been applied to the master branch.

commit d92df90470a059f0ea231250d85ab47b72bc4791
Author: Heiko Hund
Date:   Sat Dec 21 23:41:36 2024 +0100

     service: add utf8to16 function that takes a size

     Signed-off-by: Heiko Hund <heiko@ist.eigentlich.net>
     Acked-by: Frank Lichtenheld <frank@lichtenheld.com>
     Acked-by: Gert Doering <gert@greenie.muc.de>
     Message-Id: <20241221224136.20984-1-gert@greenie.muc.de>
     URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg30158.html
     Signed-off-by: Gert Doering <gert@greenie.muc.de>


--
kind regards,

Gert Doering

Patch

diff --git a/src/openvpnserv/common.c b/src/openvpnserv/common.c
index 96bf800..61a7296 100644
--- a/src/openvpnserv/common.c
+++ b/src/openvpnserv/common.c
@@ -247,17 +247,16 @@ 
     return error;
 }
 
-/* Convert a utf8 string to utf16. Caller should free the result */
 wchar_t *
-utf8to16(const char *utf8)
+utf8to16_size(const char *utf8, int size)
 {
-    int n = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
+    int n = MultiByteToWideChar(CP_UTF8, 0, utf8, size, NULL, 0);
     wchar_t *utf16 = malloc(n * sizeof(wchar_t));
     if (!utf16)
     {
         return NULL;
     }
-    MultiByteToWideChar(CP_UTF8, 0, utf8, -1, utf16, n);
+    MultiByteToWideChar(CP_UTF8, 0, utf8, size, utf16, n);
     return utf16;
 }
 
diff --git a/src/openvpnserv/service.h b/src/openvpnserv/service.h
index 6d0827d..6b559a5 100644
--- a/src/openvpnserv/service.h
+++ b/src/openvpnserv/service.h
@@ -89,8 +89,40 @@ 
 
 DWORD MsgToEventLog(DWORD flags, LPCTSTR lpszMsg, ...);
 
-/* Convert a utf8 string to utf16. Caller should free the result */
-wchar_t *utf8to16(const char *utf8);
+/**
+ * Convert a UTF-8 string to UTF-16
+ *
+ * The size parameter can be used to convert strings which contain inline NUL
+ * characters, like MULTI_SZ strings used as values in the registry do,
+ * or (sub)strings that are not zero terminated. If size is -1 the length
+ * of the string is determined automatically by the WIN32 API. Make sure
+ * you pass a terminated string or else bad things will happen. Note that
+ * the size you pass should always include the terminating zero as well.
+ *
+ * If the returned string is not NULL it must be freed by the caller.
+ *
+ * @param utf8  const string to be converted
+ * @param size  the size of the string
+ *
+ * @return wchar_t* heap allocated result string
+ */
+wchar_t *utf8to16_size(const char *utf8, int size);
+
+/**
+ * Convert a zero terminated UTF-8 string to UTF-16
+ *
+ * This is just a wrapper function that always passes -1 as string size
+ * to \ref utf8to16_size.
+ *
+ * @param utf8  const string to be converted
+ *
+ * @return wchar_t* heap allocated result string
+ */
+static inline wchar_t *
+utf8to16(const char *utf8)
+{
+    return utf8to16_size(utf8, -1);
+}
 
 /* return windows system directory as a pointer to a static string */
 const wchar_t *get_win_sys_path(void);