[Openvpn-devel] Uniform swprintf() across MinGW and MSVC compilers

Message ID 20171013095008.8288-1-simon@rozman.si
State Accepted
Headers show
Series [Openvpn-devel] Uniform swprintf() across MinGW and MSVC compilers | expand

Commit Message

Simon Rozman Oct. 12, 2017, 10:50 p.m. UTC
Legacy _snwprintf() and snwprintf() functions replaced with ISO C
swprintf().

Assigning _snwprintf() return value to unused variable was also removed
at one occasion.
---
 src/openvpn/tun.c             |  2 +-
 src/openvpnserv/interactive.c | 20 ++++++++++----------
 src/openvpnserv/validate.c    |  2 +-
 3 files changed, 12 insertions(+), 12 deletions(-)

Comments

Selva Nair Oct. 17, 2017, 5:56 a.m. UTC | #1
Hi

On Fri, Oct 13, 2017 at 5:50 AM, Simon Rozman <simon@rozman.si> wrote:

> Legacy _snwprintf() and snwprintf() functions replaced with ISO C
> swprintf().
>
> Assigning _snwprintf() return value to unused variable was also removed
> at one occasion.
> ---
>  src/openvpn/tun.c             |  2 +-
>  src/openvpnserv/interactive.c | 20 ++++++++++----------
>  src/openvpnserv/validate.c    |  2 +-
>  3 files changed, 12 insertions(+), 12 deletions(-)
>
> diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c
> index 3639718..25831ce 100644
> --- a/src/openvpn/tun.c
> +++ b/src/openvpn/tun.c
> @@ -4622,7 +4622,7 @@ get_adapter_index_method_1(const char *guid)
>      DWORD index;
>      ULONG aindex;
>      wchar_t wbuf[256];
> -    _snwprintf(wbuf, SIZE(wbuf), L"\\DEVICE\\TCPIP_%S", guid);
> +    swprintf(wbuf, SIZE(wbuf), L"\\DEVICE\\TCPIP_%S", guid);
>      wbuf [SIZE(wbuf) - 1] = 0;
>      if (GetAdapterIndex(wbuf, &aindex) != NO_ERROR)
>      {
> diff --git a/src/openvpnserv/interactive.c b/src/openvpnserv/interactive.c
> index 0b57eb9..0169617 100644
> --- a/src/openvpnserv/interactive.c
> +++ b/src/openvpnserv/interactive.c
> @@ -277,7 +277,7 @@ ReturnProcessId(HANDLE pipe, DWORD pid, DWORD count,
> LPHANDLE events)
>       * Same format as error messages (3 line string) with error = 0 in
>       * 0x%08x format, PID on line 2 and a description "Process ID" on
> line 3
>       */
> -    _snwprintf(buf, _countof(buf), L"0x%08x\n0x%08x\n%s", 0, pid, msg);
> +    swprintf(buf, _countof(buf), L"0x%08x\n0x%08x\n%s", 0, pid, msg);
>      buf[_countof(buf) - 1] = '\0';
>
>      WritePipeAsync(pipe, buf, wcslen(buf) * 2, count, events);
> @@ -403,8 +403,8 @@ ValidateOptions(HANDLE pipe, const WCHAR *workdir,
> const WCHAR *options)
>
>          if (!CheckOption(workdir, 2, argv_tmp, &settings))
>          {
> -            snwprintf(buf, _countof(buf), msg1, argv[0], workdir,
> -                      settings.ovpn_admin_group);
> +            swprintf(buf, _countof(buf), msg1, argv[0], workdir,
> +                     settings.ovpn_admin_group);
>              buf[_countof(buf) - 1] = L'\0';
>              ReturnError(pipe, ERROR_STARTUP_DATA, buf, 1, &exit_event);
>          }
> @@ -422,15 +422,15 @@ ValidateOptions(HANDLE pipe, const WCHAR *workdir,
> const WCHAR *options)
>          {
>              if (wcscmp(L"--config", argv[i]) == 0 && argc-i > 1)
>              {
> -                snwprintf(buf, _countof(buf), msg1, argv[i+1], workdir,
> -                          settings.ovpn_admin_group);
> +                swprintf(buf, _countof(buf), msg1, argv[i+1], workdir,
> +                         settings.ovpn_admin_group);
>                  buf[_countof(buf) - 1] = L'\0';
>                  ReturnError(pipe, ERROR_STARTUP_DATA, buf, 1,
> &exit_event);
>              }
>              else
>              {
> -                snwprintf(buf, _countof(buf), msg2, argv[i],
> -                          settings.ovpn_admin_group);
> +                swprintf(buf, _countof(buf), msg2, argv[i],
> +                         settings.ovpn_admin_group);
>                  buf[_countof(buf) - 1] = L'\0';
>                  ReturnError(pipe, ERROR_STARTUP_DATA, buf, 1,
> &exit_event);
>              }
> @@ -1067,7 +1067,7 @@ RegisterDNS(LPVOID unused)
>
>      if (GetSystemDirectory(sys_path, MAX_PATH))
>      {
> -        _snwprintf(ipcfg, MAX_PATH, L"%s\\%s", sys_path, L"ipconfig.exe");
> +        swprintf(ipcfg, MAX_PATH, L"%s\\%s", sys_path, L"ipconfig.exe");
>          ipcfg[MAX_PATH-1] = L'\0';
>      }
>
> @@ -1707,8 +1707,8 @@ RunOpenvpn(LPVOID p)
>      else if (exit_code != 0)
>      {
>          WCHAR buf[256];
> -        int len = _snwprintf(buf, _countof(buf),
> -                             L"OpenVPN exited with error: exit code =
> %lu", exit_code);
> +        swprintf(buf, _countof(buf),
> +                 L"OpenVPN exited with error: exit code = %lu",
> exit_code);
>          buf[_countof(buf) - 1] =  L'\0';
>          ReturnError(pipe, ERROR_OPENVPN_STARTUP, buf, 1, &exit_event);
>      }
> diff --git a/src/openvpnserv/validate.c b/src/openvpnserv/validate.c
> index f6a97e9..653bd12 100644
> --- a/src/openvpnserv/validate.c
> +++ b/src/openvpnserv/validate.c
> @@ -65,7 +65,7 @@ CheckConfigPath(const WCHAR *workdir, const WCHAR
> *fname, const settings_t *s)
>      /* convert fname to full path */
>      if (PathIsRelativeW(fname) )
>      {
> -        snwprintf(tmp, _countof(tmp), L"%s\\%s", workdir, fname);
> +        swprintf(tmp, _countof(tmp), L"%s\\%s", workdir, fname);
>          tmp[_countof(tmp)-1] = L'\0';
>          config_file = tmp;
>      }
>

Apart from making MSVC compliant,  this unifies varied usages of
snwprintf's in the code, converts all into the standard swprintf, and still
works with mingw-w64.

Compile tested using mingw-w64 4.0.4 in ubuntu 16.04 and using
3.1.0 in ubuntu 14.04[*]

ACK

Selva

[*] Ubuntu 14.04 needs some tweaks to correctly compile and link
for reasons unrelated to this patch.
<div dir="ltr">Hi<br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Oct 13, 2017 at 5:50 AM, Simon Rozman <span dir="ltr">&lt;<a href="mailto:simon@rozman.si" target="_blank">simon@rozman.si</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Legacy _snwprintf() and snwprintf() functions replaced with ISO C<br>
swprintf().<br>
<br>
Assigning _snwprintf() return value to unused variable was also removed<br>
at one occasion.<br>
---<br>
 src/openvpn/tun.c             |  2 +-<br>
 src/openvpnserv/interactive.c | 20 ++++++++++----------<br>
 src/openvpnserv/validate.c    |  2 +-<br>
 3 files changed, 12 insertions(+), 12 deletions(-)<br>
<br>
diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c<br>
index 3639718..25831ce 100644<br>
--- a/src/openvpn/tun.c<br>
+++ b/src/openvpn/tun.c<br>
@@ -4622,7 +4622,7 @@ get_adapter_index_method_1(con<wbr>st char *guid)<br>
     DWORD index;<br>
     ULONG aindex;<br>
     wchar_t wbuf[256];<br>
-    _snwprintf(wbuf, SIZE(wbuf), L&quot;\\DEVICE\\TCPIP_%S&quot;, guid);<br>
+    swprintf(wbuf, SIZE(wbuf), L&quot;\\DEVICE\\TCPIP_%S&quot;, guid);<br>
     wbuf [SIZE(wbuf) - 1] = 0;<br>
     if (GetAdapterIndex(wbuf, &amp;aindex) != NO_ERROR)<br>
     {<br>
diff --git a/src/openvpnserv/interactive.<wbr>c b/src/openvpnserv/interactive.<wbr>c<br>
index 0b57eb9..0169617 100644<br>
--- a/src/openvpnserv/interactive.<wbr>c<br>
+++ b/src/openvpnserv/interactive.<wbr>c<br>
@@ -277,7 +277,7 @@ ReturnProcessId(HANDLE pipe, DWORD pid, DWORD count, LPHANDLE events)<br>
      * Same format as error messages (3 line string) with error = 0 in<br>
      * 0x%08x format, PID on line 2 and a description &quot;Process ID&quot; on line 3<br>
      */<br>
-    _snwprintf(buf, _countof(buf), L&quot;0x%08x\n0x%08x\n%s&quot;, 0, pid, msg);<br>
+    swprintf(buf, _countof(buf), L&quot;0x%08x\n0x%08x\n%s&quot;, 0, pid, msg);<br>
     buf[_countof(buf) - 1] = &#39;\0&#39;;<br>
<br>
     WritePipeAsync(pipe, buf, wcslen(buf) * 2, count, events);<br>
@@ -403,8 +403,8 @@ ValidateOptions(HANDLE pipe, const WCHAR *workdir, const WCHAR *options)<br>
<br>
         if (!CheckOption(workdir, 2, argv_tmp, &amp;settings))<br>
         {<br>
-            snwprintf(buf, _countof(buf), msg1, argv[0], workdir,<br>
-                      settings.ovpn_admin_group);<br>
+            swprintf(buf, _countof(buf), msg1, argv[0], workdir,<br>
+                     settings.ovpn_admin_group);<br>
             buf[_countof(buf) - 1] = L&#39;\0&#39;;<br>
             ReturnError(pipe, ERROR_STARTUP_DATA, buf, 1, &amp;exit_event);<br>
         }<br>
@@ -422,15 +422,15 @@ ValidateOptions(HANDLE pipe, const WCHAR *workdir, const WCHAR *options)<br>
         {<br>
             if (wcscmp(L&quot;--config&quot;, argv[i]) == 0 &amp;&amp; argc-i &gt; 1)<br>
             {<br>
-                snwprintf(buf, _countof(buf), msg1, argv[i+1], workdir,<br>
-                          settings.ovpn_admin_group);<br>
+                swprintf(buf, _countof(buf), msg1, argv[i+1], workdir,<br>
+                         settings.ovpn_admin_group);<br>
                 buf[_countof(buf) - 1] = L&#39;\0&#39;;<br>
                 ReturnError(pipe, ERROR_STARTUP_DATA, buf, 1, &amp;exit_event);<br>
             }<br>
             else<br>
             {<br>
-                snwprintf(buf, _countof(buf), msg2, argv[i],<br>
-                          settings.ovpn_admin_group);<br>
+                swprintf(buf, _countof(buf), msg2, argv[i],<br>
+                         settings.ovpn_admin_group);<br>
                 buf[_countof(buf) - 1] = L&#39;\0&#39;;<br>
                 ReturnError(pipe, ERROR_STARTUP_DATA, buf, 1, &amp;exit_event);<br>
             }<br>
@@ -1067,7 +1067,7 @@ RegisterDNS(LPVOID unused)<br>
<br>
     if (GetSystemDirectory(sys_path, MAX_PATH))<br>
     {<br>
-        _snwprintf(ipcfg, MAX_PATH, L&quot;%s\\%s&quot;, sys_path, L&quot;ipconfig.exe&quot;);<br>
+        swprintf(ipcfg, MAX_PATH, L&quot;%s\\%s&quot;, sys_path, L&quot;ipconfig.exe&quot;);<br>
         ipcfg[MAX_PATH-1] = L&#39;\0&#39;;<br>
     }<br>
<br>
@@ -1707,8 +1707,8 @@ RunOpenvpn(LPVOID p)<br>
     else if (exit_code != 0)<br>
     {<br>
         WCHAR buf[256];<br>
-        int len = _snwprintf(buf, _countof(buf),<br>
-                             L&quot;OpenVPN exited with error: exit code = %lu&quot;, exit_code);<br>
+        swprintf(buf, _countof(buf),<br>
+                 L&quot;OpenVPN exited with error: exit code = %lu&quot;, exit_code);<br>
         buf[_countof(buf) - 1] =  L&#39;\0&#39;;<br>
         ReturnError(pipe, ERROR_OPENVPN_STARTUP, buf, 1, &amp;exit_event);<br>
     }<br>
diff --git a/src/openvpnserv/validate.c b/src/openvpnserv/validate.c<br>
index f6a97e9..653bd12 100644<br>
--- a/src/openvpnserv/validate.c<br>
+++ b/src/openvpnserv/validate.c<br>
@@ -65,7 +65,7 @@ CheckConfigPath(const WCHAR *workdir, const WCHAR *fname, const settings_t *s)<br>
     /* convert fname to full path */<br>
     if (PathIsRelativeW(fname) )<br>
     {<br>
-        snwprintf(tmp, _countof(tmp), L&quot;%s\\%s&quot;, workdir, fname);<br>
+        swprintf(tmp, _countof(tmp), L&quot;%s\\%s&quot;, workdir, fname);<br>
         tmp[_countof(tmp)-1] = L&#39;\0&#39;;<br>
         config_file = tmp;<br>
     }<br></blockquote><div><br></div><div>Apart from making MSVC compliant,  this unifies varied usages of</div><div>snwprintf&#39;s in the code, converts all into the standard swprintf, and still</div><div>works with mingw-w64. </div><div><br></div><div>Compile tested using mingw-w64 4.0.4 in ubuntu 16.04 and using </div><div>3.1.0 in ubuntu 14.04[*]</div><div><br></div><div>ACK</div><div><br></div><div>Selva</div><div><br></div><div>[*] Ubuntu 14.04 needs some tweaks to correctly compile and link</div><div>for reasons unrelated to this patch.</div></div></div></div>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
Gert Doering Nov. 4, 2017, 8:21 a.m. UTC | #2
Your patch has been applied to the master and release/2.4 branch.

commit 2f7b59196f55d62386cbcb2a889381e91e6c5148 (master)
commit 7e36553aa6f0c5ca1944025e0bd09aacb38a41cb (release/2.4)
Author: Simon Rozman
Date:   Fri Oct 13 11:50:08 2017 +0200

     Uniform swprintf() across MinGW and MSVC compilers

     Acked-by: Selva Nair <selva.nair@gmail.com>
     Message-Id: <20171013095008.8288-1-simon@rozman.si>
     URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg15633.html
     Signed-off-by: Gert Doering <gert@greenie.muc.de>


--
kind regards,

Gert Doering


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot

Patch

diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c
index 3639718..25831ce 100644
--- a/src/openvpn/tun.c
+++ b/src/openvpn/tun.c
@@ -4622,7 +4622,7 @@  get_adapter_index_method_1(const char *guid)
     DWORD index;
     ULONG aindex;
     wchar_t wbuf[256];
-    _snwprintf(wbuf, SIZE(wbuf), L"\\DEVICE\\TCPIP_%S", guid);
+    swprintf(wbuf, SIZE(wbuf), L"\\DEVICE\\TCPIP_%S", guid);
     wbuf [SIZE(wbuf) - 1] = 0;
     if (GetAdapterIndex(wbuf, &aindex) != NO_ERROR)
     {
diff --git a/src/openvpnserv/interactive.c b/src/openvpnserv/interactive.c
index 0b57eb9..0169617 100644
--- a/src/openvpnserv/interactive.c
+++ b/src/openvpnserv/interactive.c
@@ -277,7 +277,7 @@  ReturnProcessId(HANDLE pipe, DWORD pid, DWORD count, LPHANDLE events)
      * Same format as error messages (3 line string) with error = 0 in
      * 0x%08x format, PID on line 2 and a description "Process ID" on line 3
      */
-    _snwprintf(buf, _countof(buf), L"0x%08x\n0x%08x\n%s", 0, pid, msg);
+    swprintf(buf, _countof(buf), L"0x%08x\n0x%08x\n%s", 0, pid, msg);
     buf[_countof(buf) - 1] = '\0';
 
     WritePipeAsync(pipe, buf, wcslen(buf) * 2, count, events);
@@ -403,8 +403,8 @@  ValidateOptions(HANDLE pipe, const WCHAR *workdir, const WCHAR *options)
 
         if (!CheckOption(workdir, 2, argv_tmp, &settings))
         {
-            snwprintf(buf, _countof(buf), msg1, argv[0], workdir,
-                      settings.ovpn_admin_group);
+            swprintf(buf, _countof(buf), msg1, argv[0], workdir,
+                     settings.ovpn_admin_group);
             buf[_countof(buf) - 1] = L'\0';
             ReturnError(pipe, ERROR_STARTUP_DATA, buf, 1, &exit_event);
         }
@@ -422,15 +422,15 @@  ValidateOptions(HANDLE pipe, const WCHAR *workdir, const WCHAR *options)
         {
             if (wcscmp(L"--config", argv[i]) == 0 && argc-i > 1)
             {
-                snwprintf(buf, _countof(buf), msg1, argv[i+1], workdir,
-                          settings.ovpn_admin_group);
+                swprintf(buf, _countof(buf), msg1, argv[i+1], workdir,
+                         settings.ovpn_admin_group);
                 buf[_countof(buf) - 1] = L'\0';
                 ReturnError(pipe, ERROR_STARTUP_DATA, buf, 1, &exit_event);
             }
             else
             {
-                snwprintf(buf, _countof(buf), msg2, argv[i],
-                          settings.ovpn_admin_group);
+                swprintf(buf, _countof(buf), msg2, argv[i],
+                         settings.ovpn_admin_group);
                 buf[_countof(buf) - 1] = L'\0';
                 ReturnError(pipe, ERROR_STARTUP_DATA, buf, 1, &exit_event);
             }
@@ -1067,7 +1067,7 @@  RegisterDNS(LPVOID unused)
 
     if (GetSystemDirectory(sys_path, MAX_PATH))
     {
-        _snwprintf(ipcfg, MAX_PATH, L"%s\\%s", sys_path, L"ipconfig.exe");
+        swprintf(ipcfg, MAX_PATH, L"%s\\%s", sys_path, L"ipconfig.exe");
         ipcfg[MAX_PATH-1] = L'\0';
     }
 
@@ -1707,8 +1707,8 @@  RunOpenvpn(LPVOID p)
     else if (exit_code != 0)
     {
         WCHAR buf[256];
-        int len = _snwprintf(buf, _countof(buf),
-                             L"OpenVPN exited with error: exit code = %lu", exit_code);
+        swprintf(buf, _countof(buf),
+                 L"OpenVPN exited with error: exit code = %lu", exit_code);
         buf[_countof(buf) - 1] =  L'\0';
         ReturnError(pipe, ERROR_OPENVPN_STARTUP, buf, 1, &exit_event);
     }
diff --git a/src/openvpnserv/validate.c b/src/openvpnserv/validate.c
index f6a97e9..653bd12 100644
--- a/src/openvpnserv/validate.c
+++ b/src/openvpnserv/validate.c
@@ -65,7 +65,7 @@  CheckConfigPath(const WCHAR *workdir, const WCHAR *fname, const settings_t *s)
     /* convert fname to full path */
     if (PathIsRelativeW(fname) )
     {
-        snwprintf(tmp, _countof(tmp), L"%s\\%s", workdir, fname);
+        swprintf(tmp, _countof(tmp), L"%s\\%s", workdir, fname);
         tmp[_countof(tmp)-1] = L'\0';
         config_file = tmp;
     }