[Openvpn-devel,v2] manage: Switch all sleep methods to unsigned

Message ID 20261117102414.31377-1-gert@greenie.muc.de
State New
Headers
Series [Openvpn-devel,v2] manage: Switch all sleep methods to unsigned |

Commit Message

Gert Doering Nov. 17, 2026, 10:24 a.m. UTC
  From: Frank Lichtenheld <frank@lichtenheld.com>

That is consistent with sleep(3).

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

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

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

Patch

diff --git a/src/openvpn/manage.c b/src/openvpn/manage.c
index ae30930..86c498e 100644
--- a/src/openvpn/manage.c
+++ b/src/openvpn/manage.c
@@ -3615,32 +3615,23 @@ 
     }
 }
 
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wsign-conversion"
-#endif
-
 /*
  * Process the management event loop for sec seconds
  */
 void
-management_event_loop_n_seconds(struct management *man, int sec)
+management_event_loop_n_seconds(struct management *man, unsigned int sec)
 {
     if (man_standalone_ok(man))
     {
         volatile int signal_received = 0;
         const bool standalone_disabled_save = man->persist.standalone_disabled;
-        time_t expire = 0;
+        time_t expire = now + sec;
 
         /* This is so M_CLIENT messages will be correctly passed through msg() */
         man->persist.standalone_disabled = false;
 
         /* set expire time */
         update_time();
-        if (sec >= 0)
-        {
-            expire = now + sec;
-        }
 
         /* if no client connection, wait for one */
         man_wait_for_client_connection(man, &signal_received, expire, 0);
@@ -3662,7 +3653,7 @@ 
                 return;
             }
             update_time();
-        } while (expire && expire > now);
+        } while (expire > now);
 
         /* revert state */
         man->persist.standalone_disabled = standalone_disabled_save;
@@ -4026,7 +4017,7 @@ 
  * Return true if the caller should not sleep for an additional time interval.
  */
 bool
-management_hold(struct management *man, int holdtime)
+management_hold(struct management *man, unsigned int holdtime)
 {
     if (management_would_hold(man))
     {
@@ -4044,7 +4035,7 @@ 
         if (!signal_received)
         {
             struct buffer out = alloc_buf_gc(128, &gc);
-            buf_printf(&out, ">HOLD:Waiting for hold release:%d", holdtime);
+            buf_printf(&out, ">HOLD:Waiting for hold release:%u", holdtime);
             man->persist.special_state_msg = BSTR(&out);
             msg(M_CLIENT, "%s", man->persist.special_state_msg);
 
@@ -4244,8 +4235,9 @@ 
 log_history_obj_init(struct log_history *h, int capacity)
 {
     CLEAR(*h);
+    ASSERT(capacity > 0);
     h->capacity = capacity;
-    ALLOC_ARRAY_CLEAR(h->array, struct log_entry, capacity);
+    ALLOC_ARRAY_CLEAR(h->array, struct log_entry, (size_t)capacity);
 }
 
 struct log_history *
@@ -4332,34 +4324,6 @@ 
 }
 
 void
-management_sleep(const int n)
-{
-    if (n < 0)
-    {
-        return;
-    }
-    else if (management)
-    {
-        management_event_loop_n_seconds(management, n);
-    }
-    else
-    {
-#ifdef _WIN32
-        win32_sleep(n);
-#else
-        if (n > 0)
-        {
-            sleep(n);
-        }
-#endif
-    }
-}
-
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic pop
-#endif
-
-void
 management_check_bytecount_client(struct context *c, struct management *man, struct timeval *timeval)
 {
     if (man->persist.callback.flags & MCF_SERVER)
@@ -4490,30 +4454,28 @@ 
     gc_free(&gc);
     return false;
 }
-#else /* ifdef ENABLE_MANAGEMENT */
+#endif /* ENABLE_MANAGEMENT */
 
 #include "win32.h"
 
-#if !defined(_WIN32) && (defined(__GNUC__) || defined(__clang__))
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wsign-conversion"
-#endif
-
 void
-management_sleep(const int n)
+management_sleep(const unsigned int n)
 {
-#ifdef _WIN32
-    win32_sleep(n);
-#else
-    if (n > 0)
+#ifdef ENABLE_MANAGEMENT
+    if (management)
     {
-        sleep(n);
+        management_event_loop_n_seconds(management, n);
     }
-#endif /* ifdef _WIN32 */
-}
-
-#if !defined(_WIN32) && (defined(__GNUC__) || defined(__clang__))
-#pragma GCC diagnostic pop
+    else
 #endif
-
-#endif /* ENABLE_MANAGEMENT */
+    {
+#ifdef _WIN32
+        win32_sleep(n);
+#else
+        if (n > 0)
+        {
+            sleep(n);
+        }
+#endif
+    }
+}
diff --git a/src/openvpn/manage.h b/src/openvpn/manage.h
index 82c0caa..697f830 100644
--- a/src/openvpn/manage.h
+++ b/src/openvpn/manage.h
@@ -387,9 +387,9 @@ 
 
 bool management_would_hold(struct management *man);
 
-bool management_hold(struct management *man, int holdtime);
+bool management_hold(struct management *man, unsigned int holdtime);
 
-void management_event_loop_n_seconds(struct management *man, int sec);
+void management_event_loop_n_seconds(struct management *man, unsigned int sec);
 
 void management_up_down(const struct management *man, const char *updown, const struct env_set *es);
 
@@ -522,6 +522,6 @@ 
  * A sleep function that services the management layer for n seconds rather
  * than doing nothing.
  */
-void management_sleep(const int n);
+void management_sleep(const unsigned int n);
 
 #endif /* ifndef MANAGE_H */
diff --git a/src/openvpn/resolve.c b/src/openvpn/resolve.c
index b8d9899..544e5ea 100644
--- a/src/openvpn/resolve.c
+++ b/src/openvpn/resolve.c
@@ -161,13 +161,13 @@ 
     }
 
     status = getaddrinfo(hostname, servname, &hints, res);
-
-    if (status != 0)                      /* parse as numeric address failed? */
+    /* parse as numeric address failed? */
+    if (status != 0)
     {
-        const int fail_wait_interval = 5; /* seconds */
+        const unsigned int fail_wait_interval = 5; /* seconds */
         /* Add +4 to cause integer division rounding up (1 + 4) = 5, (0+4)/5=0 */
         int resolve_retries =
-            (flags & GETADDR_TRY_ONCE) ? 1 : ((resolve_retry_seconds + 4) / fail_wait_interval);
+            (flags & GETADDR_TRY_ONCE) ? 1 : ((resolve_retry_seconds + 4) / (int)fail_wait_interval);
         const char *fmt;
         msglvl_t level = 0;
 
diff --git a/src/openvpn/win32.c b/src/openvpn/win32.c
index 079869f..f10c16d 100644
--- a/src/openvpn/win32.c
+++ b/src/openvpn/win32.c
@@ -1533,13 +1533,8 @@ 
 }
 
 void
-win32_sleep(const int n)
+win32_sleep(const unsigned int n)
 {
-    if (n < 0)
-    {
-        return;
-    }
-
     /* Sleep() is not interruptible. Use a WAIT_OBJECT to catch signal */
 
     if (!HANDLE_DEFINED(win32_signal.in.read))
diff --git a/src/openvpn/win32.h b/src/openvpn/win32.h
index 39ff7d8..8152e71 100644
--- a/src/openvpn/win32.h
+++ b/src/openvpn/win32.h
@@ -323,7 +323,7 @@ 
 int openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned int flags);
 
 /* Sleep that can be interrupted by signals and exit event */
-void win32_sleep(const int n);
+void win32_sleep(const unsigned int n);
 
 /**
  * @brief Fetches a registry value for OpenVPN registry key.
diff --git a/tests/unit_tests/openvpn/mock_management.c b/tests/unit_tests/openvpn/mock_management.c
index f572dbb..aea43da 100644
--- a/tests/unit_tests/openvpn/mock_management.c
+++ b/tests/unit_tests/openvpn/mock_management.c
@@ -59,6 +59,6 @@ 
 #endif
 
 void
-management_sleep(const int n)
+management_sleep(const unsigned int n)
 {
 }