From patchwork Fri Sep 19 17:38:32 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gert Doering X-Patchwork-Id: 4430 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:7000:6a92:b0:72f:f16c:e055 with SMTP id u18csp444968maw; Fri, 19 Sep 2025 13:02:52 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXufQ0WN1gfHUYgRnxQE6izuORWHyKa5+/S6ONEZxGj0of2fCtMEjSvNFj6XjZZKulsdYGsYFnl9j0=@openvpn.net X-Google-Smtp-Source: AGHT+IH9nWneooAKDCG1nZeK/hPJYs9g56GKlg5GNoKb7iuDoFDz2otaoOrC0NAdAb+ph6KlN42P X-Received: by 2002:a05:6870:70a1:b0:322:5c2d:58d4 with SMTP id 586e51a60fabf-33bb3acbf15mr2112267fac.8.1758312172570; Fri, 19 Sep 2025 13:02:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1758312172; cv=none; d=google.com; s=arc-20240605; b=EgHIC0uWAFBKG3XXkO1cGkwPOS6TI7Zz20iAE77w2nBNWHIDFIx1fEl35ThomjwGW1 tnrCS69u0DEYreduJS0iOxRSnm9QABnou+gkq/kJcu/mqrHh3f9SzXYsCAQ8lRKe7To1 Y2ob5muRvAJSV929NXKaBtQU47GtffLhivTyrqwDoz4YcOPhdu0ZkQbsgC9USrOqQQPt Fh9vuNiaa4ZFNetHxvpaJ9HIzFbQngVvbjH7GLFvcSYqd18d3qdgkOFNu0AqaaXJ8VnW 46YE27KwHDKRquTBEZFPd2AfJRmczd4DxX+fWrUpAtCjgQWnsno9ERuOh1Tt985Nrsqi sWTw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:references:in-reply-to:message-id:date:to:from :dkim-signature:dkim-signature:dkim-signature; bh=24RZrCqg+pbCmqQyiHIIb8wsjN2g3x4uZRpdocsHprI=; fh=4NbAC/LsuMLI0S0hprUlLSLCiHwg6SCAifhH718Jh0Q=; b=PALM0ROhGTuB5ZG94VJ07E5iLywJy6/menphkFfZ4060ptFRxumioTFZwvRVOeF1dy xvmqgTSDw4Mtmer9SHCQyDl2ZRDVcjGUVnP5Wydk4wjJvN5j3cTxXIJwfaGN4MIT7j66 mtlQ1+oGdF0k9GCbmcWoN+BVzvQ6ydIBdAXd6tqtlCMTkApFXzyfBebQo2GGsqn94lfY x6hg7twY1EudPtG+Fkeotd1Z747AakQ82p0I5stIQAuJxyRdzawZMQN7TpM+O1YEgfy9 nIbFca+AXXQUCM3DGLRYSwMrOWA/9G2B67HVr1pkOqj5czhOJAMF5y/NaBci5Ki08ATi Ougw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b=N2bIBBoQ; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=cjmpdcBB; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=bpZt52yC; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=muc.de Received: from lists.sourceforge.net (lists.sourceforge.net. [216.105.38.7]) by mx.google.com with ESMTPS id 586e51a60fabf-336e67988a8si1959710fac.243.2025.09.19.13.02.52 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 19 Sep 2025 13:02:52 -0700 (PDT) Received-SPF: pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) client-ip=216.105.38.7; Authentication-Results: mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b=N2bIBBoQ; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=cjmpdcBB; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=bpZt52yC; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=muc.de DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.sourceforge.net; s=beta; h=Content-Transfer-Encoding:Content-Type: List-Subscribe:List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id: Subject:MIME-Version:References:In-Reply-To:Message-ID:Date:To:From:Sender: Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=24RZrCqg+pbCmqQyiHIIb8wsjN2g3x4uZRpdocsHprI=; b=N2bIBBoQVbMD938Gw6T1DKWXX+ zZC1ry1m9uBk0elqWqhIy1ye49TLd12TenpufAgOp82hMIDneC9PHsNeyjpdWs1XNiGrkqCZ38yzZ 8+61uwYTtefEvuuX5B4HD2yrIFKBxZhcJh1M8fFaRiWzkj0WFVreAIocmhvD9L84qsog=; Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1uzhJf-0007m1-VF; Fri, 19 Sep 2025 20:02:48 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1uzhJa-0007lQ-Fu for openvpn-devel@lists.sourceforge.net; Fri, 19 Sep 2025 20:02:43 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=V3I9/B+RFbV0xRofKPVG69FpEksR4o0LLXda8o0b01k=; b=cjmpdcBBXNC7cg4VEOtSGNLHpF /jRL+n8M7ixSCF+67DvN5GAT07ol4VXB24srOWfIjP3nRrllDeByGpEWgS1HQm+Lrzsb3LLNugjIS msy//fvNtleKrcLIrbjxQr0c6rIeP0J/iQvdTfo7LZVH3DSsaiE+CF+irWz23Kq9Bfrw=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=V3I9/B+RFbV0xRofKPVG69FpEksR4o0LLXda8o0b01k=; b=bpZt52yCWQyuPrPbgg8sba6r4j ywQEUX1i0xIccq7xTB1f3WWbqakGglFjQMcPnxr4lnlzcKUDqspBtn3oRHryWtw3lfoXpuzchDfiT uKEeXLtzwb0/2HjOxF8Rk3sovJdV8omlTnyfpPgjJ1aIydBre6Tg4m2gQHPTgXp3j7/o=; Received: from [193.149.48.134] (helo=blue.greenie.muc.de) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.95) id 1uzhJZ-0000Mq-Us for openvpn-devel@lists.sourceforge.net; Fri, 19 Sep 2025 20:02:43 +0000 Received: from blue.greenie.muc.de (localhost [127.0.0.1]) by blue.greenie.muc.de (8.18.1/8.18.1) with ESMTP id 58JHcciO028107 for ; Fri, 19 Sep 2025 19:38:38 +0200 Received: (from gert@localhost) by blue.greenie.muc.de (8.18.1/8.18.1/Submit) id 58JHccKB028106 for openvpn-devel@lists.sourceforge.net; Fri, 19 Sep 2025 19:38:38 +0200 From: Gert Doering To: openvpn-devel@lists.sourceforge.net Date: Fri, 19 Sep 2025 19:38:32 +0200 Message-ID: <20250919173838.28092-1-gert@greenie.muc.de> X-Mailer: git-send-email 2.49.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Score: 1.3 (+) X-Spam-Report: Spam detection software, running on the system "sfi-spamd-1.hosts.colo.sdot.me", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Frank Lichtenheld Really no use in having the indices and limits in int. Change-Id: I3334465738fb1fbf508dfd719b6a238b500cc0ae Signed-off-by: Frank Lichtenheld Acked-by: Gert Doering Gerrit URL: https://gerrit.openvpn.net/c/open [...] Content analysis details: (1.3 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 1.3 RDNS_NONE Delivered to internal network by a host with no rDNS X-Headers-End: 1uzhJZ-0000Mq-Us Subject: [Openvpn-devel] [PATCH v10] list: Make types of hash elements consistent X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox X-GMAIL-THRID: =?utf-8?q?1843723944340592139?= X-GMAIL-MSGID: =?utf-8?q?1843723944340592139?= From: Frank Lichtenheld Really no use in having the indices and limits in int. Change-Id: I3334465738fb1fbf508dfd719b6a238b500cc0ae Signed-off-by: Frank Lichtenheld Acked-by: Gert Doering Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1119 --- 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/+/1119 This mail reflects revision 10 of this Change. Acked-by according to Gerrit (reflected above): Gert Doering diff --git a/src/openvpn/integer.h b/src/openvpn/integer.h index b82379e..2a6f14a 100644 --- a/src/openvpn/integer.h +++ b/src/openvpn/integer.h @@ -135,6 +135,27 @@ } } +static inline unsigned int +constrain_uint(unsigned int x, unsigned int min, unsigned int max) +{ + if (min > max) + { + return min; + } + if (x < min) + { + return min; + } + else if (x > max) + { + return max; + } + else + { + return x; + } +} + /* * Functions used for circular buffer index arithmetic. */ diff --git a/src/openvpn/list.c b/src/openvpn/list.c index f30d540..1b90d82 100644 --- a/src/openvpn/list.c +++ b/src/openvpn/list.c @@ -34,22 +34,21 @@ #include "memdbg.h" struct hash * -hash_init(const int n_buckets, const uint32_t iv, +hash_init(const uint32_t n_buckets, const uint32_t iv, uint32_t (*hash_function)(const void *key, uint32_t iv), bool (*compare_function)(const void *key1, const void *key2)) { struct hash *h; - int i; ASSERT(n_buckets > 0); ALLOC_OBJ_CLEAR(h, struct hash); - h->n_buckets = (int)adjust_power_of_2(n_buckets); + h->n_buckets = (uint32_t)adjust_power_of_2(n_buckets); h->mask = h->n_buckets - 1; h->hash_function = hash_function; h->compare_function = compare_function; h->iv = iv; ALLOC_ARRAY(h->buckets, struct hash_bucket, h->n_buckets); - for (i = 0; i < h->n_buckets; ++i) + for (uint32_t i = 0; i < h->n_buckets; ++i) { struct hash_bucket *b = &h->buckets[i]; b->list = NULL; @@ -60,8 +59,7 @@ void hash_free(struct hash *hash) { - int i; - for (i = 0; i < hash->n_buckets; ++i) + for (uint32_t i = 0; i < hash->n_buckets; ++i) { struct hash_bucket *b = &hash->buckets[i]; struct hash_element *he = b->list; @@ -212,15 +210,15 @@ } void -hash_iterator_init_range(struct hash *hash, struct hash_iterator *hi, int start_bucket, - int end_bucket) +hash_iterator_init_range(struct hash *hash, struct hash_iterator *hi, uint32_t start_bucket, + uint32_t end_bucket) { if (end_bucket > hash->n_buckets) { end_bucket = hash->n_buckets; } - ASSERT(start_bucket >= 0 && start_bucket <= end_bucket); + ASSERT(start_bucket <= end_bucket); hi->hash = hash; hi->elem = NULL; @@ -325,6 +323,9 @@ * the return value. Every 1-bit and 2-bit delta achieves avalanche. * About 36+6len instructions. * + * #define hashsize(n) ((uint32_t)1<<(n)) + * #define hashmask(n) (hashsize(n)-1) + * * The best hash table sizes are powers of 2. There is no need to do * mod a prime (mod is sooo slow!). If you need less than 32 bits, * use a bitmask. For example, if you need only 10 bits, do diff --git a/src/openvpn/list.h b/src/openvpn/list.h index fb3302d..5702b95 100644 --- a/src/openvpn/list.h +++ b/src/openvpn/list.h @@ -36,14 +36,11 @@ #include "basic.h" #include "buffer.h" -#define hashsize(n) ((uint32_t)1 << (n)) -#define hashmask(n) (hashsize(n) - 1) - struct hash_element { void *value; const void *key; - unsigned int hash_value; + uint32_t hash_value; struct hash_element *next; }; @@ -54,16 +51,16 @@ struct hash { - int n_buckets; - int n_elements; - int mask; + uint32_t n_buckets; + uint32_t n_elements; + uint32_t mask; uint32_t iv; uint32_t (*hash_function)(const void *key, uint32_t iv); bool (*compare_function)(const void *key1, const void *key2); /* return true if equal */ struct hash_bucket *buckets; }; -struct hash *hash_init(const int n_buckets, const uint32_t iv, +struct hash *hash_init(const uint32_t n_buckets, const uint32_t iv, uint32_t (*hash_function)(const void *key, uint32_t iv), bool (*compare_function)(const void *key1, const void *key2)); @@ -81,17 +78,17 @@ struct hash_iterator { struct hash *hash; - int bucket_index; + uint32_t bucket_index; struct hash_bucket *bucket; struct hash_element *elem; struct hash_element *last; bool bucket_marked; - int bucket_index_start; - int bucket_index_end; + uint32_t bucket_index_start; + uint32_t bucket_index_end; }; -void hash_iterator_init_range(struct hash *hash, struct hash_iterator *hi, int start_bucket, - int end_bucket); +void hash_iterator_init_range(struct hash *hash, struct hash_iterator *hi, uint32_t start_bucket, + uint32_t end_bucket); void hash_iterator_init(struct hash *hash, struct hash_iterator *iter); @@ -109,13 +106,13 @@ return (*hash->hash_function)(key, hash->iv); } -static inline int +static inline uint32_t hash_n_elements(const struct hash *hash) { return hash->n_elements; } -static inline int +static inline uint32_t hash_n_buckets(const struct hash *hash) { return hash->n_buckets; diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index 8676a09..9256127 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -169,18 +169,12 @@ } static void -multi_reap_range(const struct multi_context *m, int start_bucket, int end_bucket) +multi_reap_range(const struct multi_context *m, uint32_t start_bucket, uint32_t end_bucket) { struct gc_arena gc = gc_new(); struct hash_iterator hi; struct hash_element *he; - if (start_bucket < 0) - { - start_bucket = 0; - end_bucket = hash_n_buckets(m->vhash); - } - dmsg(D_MULTI_DEBUG, "MULTI: REAP range %d -> %d", start_bucket, end_bucket); hash_iterator_init_range(m->vhash, &hi, start_bucket, end_bucket); while ((he = hash_iterator_next(&hi)) != NULL) @@ -201,11 +195,11 @@ static void multi_reap_all(const struct multi_context *m) { - multi_reap_range(m, -1, 0); + multi_reap_range(m, 0, hash_n_buckets(m->vhash)); } static struct multi_reap * -multi_reap_new(int buckets_per_pass) +multi_reap_new(uint32_t buckets_per_pass) { struct multi_reap *mr; ALLOC_OBJ(mr, struct multi_reap); @@ -237,10 +231,10 @@ /* * How many buckets in vhash to reap per pass. */ -static int -reap_buckets_per_pass(int n_buckets) +static uint32_t +reap_buckets_per_pass(uint32_t n_buckets) { - return constrain_int(n_buckets / REAP_DIVISOR, REAP_MIN, REAP_MAX); + return constrain_uint(n_buckets / REAP_DIVISOR, REAP_MIN, REAP_MAX); } #ifdef ENABLE_MANAGEMENT diff --git a/src/openvpn/multi.h b/src/openvpn/multi.h index 087c0e6..b2b892b 100644 --- a/src/openvpn/multi.h +++ b/src/openvpn/multi.h @@ -51,8 +51,8 @@ */ struct multi_reap { - int bucket_base; - int buckets_per_pass; + uint32_t bucket_base; + uint32_t buckets_per_pass; time_t last_call; }; diff --git a/src/openvpn/options.c b/src/openvpn/options.c index f1f66b9..151a016 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -7956,8 +7956,8 @@ { goto err; } - options->real_hash_size = real; - options->virtual_hash_size = virtual; + options->real_hash_size = (uint32_t)real; + options->virtual_hash_size = (uint32_t)virtual; } else if (streq(p[0], "connect-freq") && p[1] && p[2] && !p[3]) { diff --git a/src/openvpn/options.h b/src/openvpn/options.h index a737711..b033068 100644 --- a/src/openvpn/options.h +++ b/src/openvpn/options.h @@ -499,8 +499,8 @@ struct in6_addr ifconfig_ipv6_pool_base; /* IPv6 */ int ifconfig_ipv6_pool_netbits; /* IPv6 */ - int real_hash_size; - int virtual_hash_size; + uint32_t real_hash_size; + uint32_t virtual_hash_size; const char *client_connect_script; const char *client_disconnect_script; const char *learn_address_script; diff --git a/tests/unit_tests/openvpn/test_misc.c b/tests/unit_tests/openvpn/test_misc.c index dbfdd56..10bed1d 100644 --- a/tests/unit_tests/openvpn/test_misc.c +++ b/tests/unit_tests/openvpn/test_misc.c @@ -128,7 +128,7 @@ word_hash_function(const void *key, uint32_t iv) { const char *str = (const char *)key; - const int len = strlen(str); + const uint32_t len = (uint32_t)strlen(str); return hash_func((const uint8_t *)str, len, iv); } @@ -138,11 +138,11 @@ return strcmp((const char *)key1, (const char *)key2) == 0; } -static unsigned long +static uint32_t get_random(void) { /* rand() is not very random, but it's C99 and this is just for testing */ - return rand(); + return (uint32_t)rand(); } static struct hash_element * @@ -176,7 +176,7 @@ struct hash *hash = hash_init(10000, get_random(), word_hash_function, word_compare_function); struct hash *nhash = hash_init(256, get_random(), word_hash_function, word_compare_function); - printf("hash_init n_buckets=%d mask=0x%08x\n", hash->n_buckets, hash->mask); + printf("hash_init n_buckets=%u mask=0x%08x\n", hash->n_buckets, hash->mask); char wordfile[PATH_MAX] = { 0 }; openvpn_test_get_srcdir_dir(wordfile, PATH_MAX, "/../../../COPYRIGHT.GPL"); @@ -254,10 +254,10 @@ /* output contents of hash table */ { - ptr_type inc = 0; + uint32_t inc = 0; int count = 0; - for (ptr_type base = 0; base < hash_n_buckets(hash); base += inc) + for (uint32_t base = 0; base < hash_n_buckets(hash); base += inc) { struct hash_iterator hi; struct hash_element *he;