]> git.ipfire.org Git - thirdparty/openvpn.git/commitdiff
Silence compiler truncation warning by checking snprintf return value
authorArne Schwabe <arne@rfc2549.org>
Wed, 21 Jan 2026 12:18:30 +0000 (13:18 +0100)
committerGert Doering <gert@greenie.muc.de>
Sat, 24 Jan 2026 15:47:48 +0000 (16:47 +0100)
On the more recent mingw compilers (homebrew mingw 13.0.0, GCC 15.2.0) the
compiler complains about a potential truncation in these two places.

  src/openvpn/tun.c:3806:57:
  error: '%s' directive output may be truncated writing up
  to 255 bytes into a region of size 178
  [-Werror=format-truncation=]

This not very helpful but checking the snprintf return value
will make the compiler not warn about this.

Change-Id: I54b11a5540fb236580a3b80c6d1e8678b24bd852
Signed-off-by: Arne Schwabe <arne@rfc2549.org>
Acked-by: Frank Lichtenheld <frank@lichtenheld.com>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1272
Message-Id: <20260121121830.27244-1-frank@lichtenheld.com>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg35367.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>
src/openvpn/tun.c

index 0399b4a137c4a4fe5cf09a0b093a693c9ad2cf5b..e38df3ede772a4cad9c8998590ef751878911bd5 100644 (file)
@@ -3558,7 +3558,13 @@ get_tap_reg(struct gc_arena *gc)
             msg(M_FATAL, "Error enumerating registry subkeys of key: %s", ADAPTER_KEY);
         }
 
-        snprintf(unit_string, sizeof(unit_string), "%s\\%s", ADAPTER_KEY, enum_name);
+        int ret = snprintf(unit_string, sizeof(unit_string), "%s\\%s", ADAPTER_KEY, enum_name);
+
+        if (ret < 0 || ret >= sizeof(unit_string))
+        {
+            msg(M_WARN, "Error constructing unit string for %s", enum_name);
+            continue;
+        }
 
         status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, unit_string, 0, KEY_READ, &unit_key);
 
@@ -3667,8 +3673,15 @@ get_panel_reg(struct gc_arena *gc)
             msg(M_FATAL, "Error enumerating registry subkeys of key: %s", NETWORK_CONNECTIONS_KEY);
         }
 
-        snprintf(connection_string, sizeof(connection_string), "%s\\%s\\Connection",
-                 NETWORK_CONNECTIONS_KEY, enum_name);
+        int ret = snprintf(connection_string, sizeof(connection_string), "%s\\%s\\Connection",
+                           NETWORK_CONNECTIONS_KEY, enum_name);
+
+        if (ret < 0 || ret >= sizeof(connection_string))
+        {
+            msg(M_WARN, "Error constructing connection string for %s", enum_name);
+            continue;
+        }
+
 
         status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, connection_string, 0, KEY_READ, &connection_key);