[Openvpn-devel,v1] console: Switch response_len to size_t

Message ID 20261110150027.17743-1-gert@greenie.muc.de
State New
Headers
Series [Openvpn-devel,v1] console: Switch response_len to size_t |

Commit Message

Gert Doering Nov. 10, 2026, 3 p.m. UTC
  From: Frank Lichtenheld <frank@lichtenheld.com>

We need some casts either way since the system
APIs are just too different. But overall it feels
cleaner when using size_t.

Change-Id: I1c87faefd3adc2d95b5318437d097c3970a120d3
Signed-off-by: Frank Lichtenheld <frank@lichtenheld.com>
Acked-by: Razvan Cojocaru <razvanc@mailbox.org>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2168
---

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/+/2168
This mail reflects revision 1 of this Change.

Acked-by according to Gerrit (reflected above):
Razvan Cojocaru <razvanc@mailbox.org>
  

Patch

diff --git a/src/openvpn/buffer.c b/src/openvpn/buffer.c
index a4e2809..05c4e30 100644
--- a/src/openvpn/buffer.c
+++ b/src/openvpn/buffer.c
@@ -576,9 +576,9 @@ 
  * like buf_null_terminate, but operate on strings
  */
 void
-string_null_terminate(char *str, int len, int capacity)
+string_null_terminate(char *str, size_t len, size_t capacity)
 {
-    ASSERT(len >= 0 && len <= capacity && capacity > 0);
+    ASSERT(len <= capacity && capacity > 0);
     if (len < capacity)
     {
         *(str + len) = '\0';
diff --git a/src/openvpn/buffer.h b/src/openvpn/buffer.h
index ae6dcb7..541f0bd 100644
--- a/src/openvpn/buffer.h
+++ b/src/openvpn/buffer.h
@@ -872,7 +872,7 @@ 
  * @param capacity  Total size of the character buffer in bytes.
  */
 #ifdef _WIN32
-void string_null_terminate(char *str, int len, int capacity);
+void string_null_terminate(char *str, size_t len, size_t capacity);
 #endif
 /**@}*/
 /* End of string utility functions */
diff --git a/src/openvpn/console.c b/src/openvpn/console.c
index 78f445b..358b9ae 100644
--- a/src/openvpn/console.c
+++ b/src/openvpn/console.c
@@ -53,7 +53,7 @@ 
 
 
 void
-query_user_add(char *prompt, char *resp, int resp_len, bool echo)
+query_user_add(char *prompt, char *resp, size_t resp_len, bool echo)
 {
     int i;
 
diff --git a/src/openvpn/console.h b/src/openvpn/console.h
index 89550ce..94b1032 100644
--- a/src/openvpn/console.h
+++ b/src/openvpn/console.h
@@ -32,10 +32,10 @@ 
  */
 struct _query_user
 {
-    char *prompt;     /**< Prompt to present to the user */
-    char *response;   /**< The user's response */
-    int response_len; /**< Length the of the user response */
-    bool echo;        /**< True: The user should see what is being typed, otherwise mask it */
+    char *prompt;        /**< Prompt to present to the user */
+    char *response;      /**< The user's response */
+    size_t response_len; /**< Length the of the user response */
+    bool echo;           /**< True: The user should see what is being typed, otherwise mask it */
 };
 
 #define QUERY_USER_NUMSLOTS 10
@@ -57,7 +57,7 @@ 
  * @param echo       Should the user input be echoed to the user?  If False, input will be masked
  *
  */
-void query_user_add(char *prompt, char *resp, int resp_len, bool echo);
+void query_user_add(char *prompt, char *resp, size_t resp_len, bool echo);
 
 
 /**
@@ -115,7 +115,7 @@ 
  *
  */
 static inline bool
-query_user_SINGLE(char *prompt, char *resp, int resp_len, bool echo)
+query_user_SINGLE(char *prompt, char *resp, size_t resp_len, bool echo)
 {
     query_user_clear();
     query_user_add(prompt, resp, resp_len, echo);
diff --git a/src/openvpn/console_builtin.c b/src/openvpn/console_builtin.c
index 0353ade..8206d3d 100644
--- a/src/openvpn/console_builtin.c
+++ b/src/openvpn/console_builtin.c
@@ -41,11 +41,6 @@ 
 #include <termios.h>
 #endif
 
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wsign-conversion"
-#endif
-
 #ifdef _WIN32
 
 #include "win32.h"
@@ -62,11 +57,11 @@ 
  *         exit event is signaled.
  */
 static bool
-get_console_input_win32(const char *prompt, const bool echo, char *input, const int capacity)
+get_console_input_win32(const char *prompt, const bool echo, char *input, const size_t capacity)
 {
     ASSERT(prompt);
     ASSERT(input);
-    ASSERT(capacity > 0);
+    ASSERT(capacity > 0 && capacity < INT_MAX);
 
     input[0] = '\0';
 
@@ -110,16 +105,16 @@ 
             return false;
         }
 
-        status = ReadConsoleW(in, winput, capacity, &len, NULL);
-        WideCharToMultiByte(CP_UTF8, 0, winput, len, input, capacity, NULL, NULL);
+        status = ReadConsoleW(in, winput, (DWORD)capacity, &len, NULL);
+        WideCharToMultiByte(CP_UTF8, 0, winput, (int)len, input, (int)capacity, NULL, NULL);
         free(winput);
     }
     else
     {
-        status = ReadFile(in, input, capacity, &len, NULL);
+        status = ReadFile(in, input, (DWORD)capacity, &len, NULL);
     }
 
-    string_null_terminate(input, (int)len, capacity);
+    string_null_terminate(input, len, capacity);
     chomp(input);
 
     if (!echo)
@@ -191,11 +186,11 @@ 
  *  @returns Returns True if user input was gathered
  */
 static bool
-get_console_input(const char *prompt, const bool echo, char *input, const int capacity)
+get_console_input(const char *prompt, const bool echo, char *input, const size_t capacity)
 {
     ASSERT(prompt);
     ASSERT(input);
-    ASSERT(capacity > 0);
+    ASSERT(capacity > 0 && capacity <= INT_MAX);
     input[0] = '\0';
 
 #ifdef _WIN32
@@ -238,7 +233,7 @@ 
         restore_tty = (tcsetattr(fileno(fp), TCSAFLUSH, &tty_tmp) == 0);
     }
 
-    if (fgets(input, capacity, fp) != NULL)
+    if (fgets(input, (int)capacity, fp) != NULL)
     {
         chomp(input);
         ret = true;
@@ -294,7 +289,3 @@ 
 
     return ret;
 }
-
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic pop
-#endif
diff --git a/src/openvpn/console_systemd.c b/src/openvpn/console_systemd.c
index 8fc7f72..f0ca7fa 100644
--- a/src/openvpn/console_systemd.c
+++ b/src/openvpn/console_systemd.c
@@ -53,13 +53,8 @@ 
     return (sd_booted() > 0) && (stat(SYSTEMD_ASK_PASSWORD_PATH, &c) == 0);
 }
 
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wsign-conversion"
-#endif
-
 static bool
-get_console_input_systemd(const char *prompt, const bool echo, char *input, const int capacity)
+get_console_input_systemd(const char *prompt, const bool echo, char *input, const size_t capacity)
 {
     int std_out;
     bool ret = false;
@@ -91,10 +86,6 @@ 
     return ret;
 }
 
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic pop
-#endif
-
 /**
  *  Systemd aware implementation of query_user_exec().  If systemd is not running
  *  it will fall back to use query_user_exec_builtin() instead.
diff --git a/tests/unit_tests/openvpn/test_user_pass.c b/tests/unit_tests/openvpn/test_user_pass.c
index 5bc1d2a..54b3617 100644
--- a/tests/unit_tests/openvpn/test_user_pass.c
+++ b/tests/unit_tests/openvpn/test_user_pass.c
@@ -47,11 +47,6 @@ 
 }
 #endif
 
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wsign-conversion"
-#endif
-
 bool
 query_user_exec_builtin(void)
 {
@@ -65,10 +60,6 @@ 
     return mock();
 }
 
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic pop
-#endif
-
 void
 management_auth_failure(const struct management *man, const char *type, const char *reason)
 {