]> git.ipfire.org Git - thirdparty/openvpn.git/commitdiff
ssl: Clean up type handling in write_string()
authorFrank Lichtenheld <frank@lichtenheld.com>
Tue, 4 Nov 2025 09:19:35 +0000 (10:19 +0100)
committerGert Doering <gert@greenie.muc.de>
Sun, 16 Nov 2025 22:03:11 +0000 (23:03 +0100)
Make better checks for the maxlen input value.

Change-Id: I3309265edf8d6bea7bd73b21eef589a92ede6e0a
Signed-off-by: Frank Lichtenheld <frank@lichtenheld.com>
Acked-by: Gert Doering <gert@greenie.muc.de>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1300
Message-Id: <20251104091940.10826-1-gert@greenie.muc.de>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg34191.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>
src/openvpn/ssl.c

index 92a68d04dcee6c9e938fa703fcadc8f48fc72daf..22a1f5251b081fe8ee4c91632c468363203e98d3 100644 (file)
@@ -1780,20 +1780,16 @@ write_empty_string(struct buffer *buf)
     return true;
 }
 
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wconversion"
-#endif
-
 static bool
 write_string(struct buffer *buf, const char *str, const int maxlen)
 {
-    const int len = strlen(str) + 1;
-    if (len < 1 || (maxlen >= 0 && len > maxlen))
+    const size_t len = strlen(str) + 1;
+    const size_t real_maxlen = (maxlen >= 0 && maxlen <= UINT16_MAX) ? (size_t)maxlen : UINT16_MAX;
+    if (len > real_maxlen)
     {
         return false;
     }
-    if (!buf_write_u16(buf, len))
+    if (!buf_write_u16(buf, (uint16_t)len))
     {
         return false;
     }
@@ -1833,6 +1829,11 @@ read_string(struct buffer *buf, char *str, const unsigned int capacity)
     return len;
 }
 
+#if defined(__GNUC__) || defined(__clang__)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wconversion"
+#endif
+
 static char *
 read_string_alloc(struct buffer *buf)
 {