]> git.ipfire.org Git - thirdparty/openvpn.git/commitdiff
buffer: Avoid sign-compare warnings
authorFrank Lichtenheld <frank@lichtenheld.com>
Mon, 30 Mar 2026 11:36:39 +0000 (13:36 +0200)
committerGert Doering <gert@greenie.muc.de>
Mon, 30 Mar 2026 12:55:32 +0000 (14:55 +0200)
- Switch buffer_list size and max_size to size_t
- Guard some unavoidable size_t -> int conversions

Change-Id: Iecc3e3d5d13cb85c1f287ad4816e1e6a7b2bcdef
Signed-off-by: Frank Lichtenheld <frank@lichtenheld.com>
Acked-by: Gert Doering <gert@greenie.muc.de>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1561
Message-Id: <20260330113648.19896-1-gert@greenie.muc.de>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg36335.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>
src/openvpn/buffer.c
src/openvpn/buffer.h

index 75110ede7698692052e4a357b5e2e5761d912536..71f3769056516272673651d55d21f1de76f9df12 100644 (file)
@@ -53,7 +53,7 @@ array_mult_safe(const size_t m1, const size_t m2, const size_t extra)
 void
 buf_size_error(const size_t size)
 {
-    msg(M_FATAL, "fatal buffer size error, size=%lu", (unsigned long)size);
+    msg(M_FATAL, "fatal buffer size error, size=%zu", size);
 }
 
 struct buffer
@@ -280,11 +280,6 @@ buf_puts(struct buffer *buf, const char *str)
     return ret;
 }
 
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wsign-compare"
-#endif
-
 /*
  * write a string to the end of a buffer that was
  * truncated by buf_printf
@@ -295,7 +290,7 @@ buf_catrunc(struct buffer *buf, const char *str)
     if (buf_forward_capacity(buf) <= 1)
     {
         size_t len = strlen(str) + 1;
-        if (len < buf_forward_capacity_total(buf))
+        if (buf_size_valid(len) && (int)len < buf_forward_capacity_total(buf))
         {
             memcpy(buf->data + buf->capacity - len, str, len);
         }
@@ -782,7 +777,7 @@ bool
 buf_string_match_head_str(const struct buffer *src, const char *match)
 {
     const size_t size = strlen(match);
-    if (size > src->len)
+    if (!buf_size_valid(size) || (int)size > src->len)
     {
         return false;
     }
@@ -1196,7 +1191,7 @@ buffer_list_free(struct buffer_list *ol)
 bool
 buffer_list_defined(const struct buffer_list *ol)
 {
-    return ol && ol->head != NULL;
+    return ol && ol->head != NULL && ol->size > 0;
 }
 
 void
@@ -1223,7 +1218,7 @@ buffer_list_push(struct buffer_list *ol, const char *str)
         struct buffer_entry *e = buffer_list_push_data(ol, str, len + 1);
         if (e)
         {
-            e->buf.len = (int)len; /* Don't count trailing '\0' as part of length */
+            e->buf.len--; /* Don't count trailing '\0' as part of length */
         }
     }
 }
@@ -1249,6 +1244,7 @@ buffer_list_push_data(struct buffer_list *ol, const void *data, size_t size)
         }
         e->buf = alloc_buf(size);
         memcpy(e->buf.data, data, size);
+        /* Note: size implicitly checked by alloc_buf */
         e->buf.len = (int)size;
         ol->tail = e;
     }
@@ -1274,7 +1270,7 @@ buffer_list_aggregate_separator(struct buffer_list *bl, const size_t max_len, co
     const size_t sep_len = strlen(sep);
     struct buffer_entry *more = bl->head;
     size_t size = 0;
-    int count = 0;
+    size_t count = 0;
     for (; more; ++count)
     {
         size_t extra_len = BLENZ(&more->buf) + sep_len;
@@ -1313,10 +1309,6 @@ buffer_list_aggregate_separator(struct buffer_list *bl, const size_t max_len, co
     }
 }
 
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic pop
-#endif
-
 void
 buffer_list_aggregate(struct buffer_list *bl, const size_t max)
 {
@@ -1326,7 +1318,7 @@ buffer_list_aggregate(struct buffer_list *bl, const size_t max)
 void
 buffer_list_pop(struct buffer_list *ol)
 {
-    if (ol && ol->head)
+    if (buffer_list_defined(ol))
     {
         struct buffer_entry *e = ol->head->next;
         free_buf(&ol->head->buf);
index 040f752ee5bef73d4787999c61a5b4dcb00069be..fcc923b77ca314f2b1b257de45b6361747192427 100644 (file)
@@ -1149,8 +1149,8 @@ struct buffer_list
 {
     struct buffer_entry *head; /* next item to pop/peek */
     struct buffer_entry *tail; /* last item pushed */
-    int size;                  /* current number of entries */
-    int max_size;              /* maximum size list should grow to */
+    size_t size;               /* current number of entries */
+    size_t max_size;           /* maximum size list should grow to */
 };
 
 /**