Skip to content

Commit a69aac4

Browse files
flichtenheldcron2
authored andcommitted
list: Make types of hash elements consistent
Really no use in having the indices and limits in int. Change-Id: I3334465738fb1fbf508dfd719b6a238b500cc0ae Signed-off-by: Frank Lichtenheld <[email protected]> Acked-by: Gert Doering <[email protected]> Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1119 Message-Id: <[email protected]> URL: https://www.mail-archive.com/[email protected]/msg33108.html Signed-off-by: Gert Doering <[email protected]>
1 parent efb58ab commit a69aac4

File tree

8 files changed

+61
-48
lines changed

8 files changed

+61
-48
lines changed

src/openvpn/integer.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,27 @@ constrain_int(int x, int min, int max)
135135
}
136136
}
137137

138+
static inline unsigned int
139+
constrain_uint(unsigned int x, unsigned int min, unsigned int max)
140+
{
141+
if (min > max)
142+
{
143+
return min;
144+
}
145+
if (x < min)
146+
{
147+
return min;
148+
}
149+
else if (x > max)
150+
{
151+
return max;
152+
}
153+
else
154+
{
155+
return x;
156+
}
157+
}
158+
138159
/*
139160
* Functions used for circular buffer index arithmetic.
140161
*/

src/openvpn/list.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,21 @@
3434
#include "memdbg.h"
3535

3636
struct hash *
37-
hash_init(const int n_buckets, const uint32_t iv,
37+
hash_init(const uint32_t n_buckets, const uint32_t iv,
3838
uint32_t (*hash_function)(const void *key, uint32_t iv),
3939
bool (*compare_function)(const void *key1, const void *key2))
4040
{
4141
struct hash *h;
42-
int i;
4342

4443
ASSERT(n_buckets > 0);
4544
ALLOC_OBJ_CLEAR(h, struct hash);
46-
h->n_buckets = (int)adjust_power_of_2(n_buckets);
45+
h->n_buckets = (uint32_t)adjust_power_of_2(n_buckets);
4746
h->mask = h->n_buckets - 1;
4847
h->hash_function = hash_function;
4948
h->compare_function = compare_function;
5049
h->iv = iv;
5150
ALLOC_ARRAY(h->buckets, struct hash_bucket, h->n_buckets);
52-
for (i = 0; i < h->n_buckets; ++i)
51+
for (uint32_t i = 0; i < h->n_buckets; ++i)
5352
{
5453
struct hash_bucket *b = &h->buckets[i];
5554
b->list = NULL;
@@ -60,8 +59,7 @@ hash_init(const int n_buckets, const uint32_t iv,
6059
void
6160
hash_free(struct hash *hash)
6261
{
63-
int i;
64-
for (i = 0; i < hash->n_buckets; ++i)
62+
for (uint32_t i = 0; i < hash->n_buckets; ++i)
6563
{
6664
struct hash_bucket *b = &hash->buckets[i];
6765
struct hash_element *he = b->list;
@@ -212,15 +210,15 @@ hash_remove_marked(struct hash *hash, struct hash_bucket *bucket)
212210
}
213211

214212
void
215-
hash_iterator_init_range(struct hash *hash, struct hash_iterator *hi, int start_bucket,
216-
int end_bucket)
213+
hash_iterator_init_range(struct hash *hash, struct hash_iterator *hi, uint32_t start_bucket,
214+
uint32_t end_bucket)
217215
{
218216
if (end_bucket > hash->n_buckets)
219217
{
220218
end_bucket = hash->n_buckets;
221219
}
222220

223-
ASSERT(start_bucket >= 0 && start_bucket <= end_bucket);
221+
ASSERT(start_bucket <= end_bucket);
224222

225223
hi->hash = hash;
226224
hi->elem = NULL;
@@ -325,6 +323,9 @@ hash_iterator_delete_element(struct hash_iterator *hi)
325323
* the return value. Every 1-bit and 2-bit delta achieves avalanche.
326324
* About 36+6len instructions.
327325
*
326+
* #define hashsize(n) ((uint32_t)1<<(n))
327+
* #define hashmask(n) (hashsize(n)-1)
328+
*
328329
* The best hash table sizes are powers of 2. There is no need to do
329330
* mod a prime (mod is sooo slow!). If you need less than 32 bits,
330331
* use a bitmask. For example, if you need only 10 bits, do

src/openvpn/list.h

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,11 @@
3636
#include "basic.h"
3737
#include "buffer.h"
3838

39-
#define hashsize(n) ((uint32_t)1 << (n))
40-
#define hashmask(n) (hashsize(n) - 1)
41-
4239
struct hash_element
4340
{
4441
void *value;
4542
const void *key;
46-
unsigned int hash_value;
43+
uint32_t hash_value;
4744
struct hash_element *next;
4845
};
4946

@@ -54,16 +51,16 @@ struct hash_bucket
5451

5552
struct hash
5653
{
57-
int n_buckets;
58-
int n_elements;
59-
int mask;
54+
uint32_t n_buckets;
55+
uint32_t n_elements;
56+
uint32_t mask;
6057
uint32_t iv;
6158
uint32_t (*hash_function)(const void *key, uint32_t iv);
6259
bool (*compare_function)(const void *key1, const void *key2); /* return true if equal */
6360
struct hash_bucket *buckets;
6461
};
6562

66-
struct hash *hash_init(const int n_buckets, const uint32_t iv,
63+
struct hash *hash_init(const uint32_t n_buckets, const uint32_t iv,
6764
uint32_t (*hash_function)(const void *key, uint32_t iv),
6865
bool (*compare_function)(const void *key1, const void *key2));
6966

@@ -81,17 +78,17 @@ void hash_remove_by_value(struct hash *hash, void *value);
8178
struct hash_iterator
8279
{
8380
struct hash *hash;
84-
int bucket_index;
81+
uint32_t bucket_index;
8582
struct hash_bucket *bucket;
8683
struct hash_element *elem;
8784
struct hash_element *last;
8885
bool bucket_marked;
89-
int bucket_index_start;
90-
int bucket_index_end;
86+
uint32_t bucket_index_start;
87+
uint32_t bucket_index_end;
9188
};
9289

93-
void hash_iterator_init_range(struct hash *hash, struct hash_iterator *hi, int start_bucket,
94-
int end_bucket);
90+
void hash_iterator_init_range(struct hash *hash, struct hash_iterator *hi, uint32_t start_bucket,
91+
uint32_t end_bucket);
9592

9693
void hash_iterator_init(struct hash *hash, struct hash_iterator *iter);
9794

@@ -109,13 +106,13 @@ hash_value(const struct hash *hash, const void *key)
109106
return (*hash->hash_function)(key, hash->iv);
110107
}
111108

112-
static inline int
109+
static inline uint32_t
113110
hash_n_elements(const struct hash *hash)
114111
{
115112
return hash->n_elements;
116113
}
117114

118-
static inline int
115+
static inline uint32_t
119116
hash_n_buckets(const struct hash *hash)
120117
{
121118
return hash->n_buckets;

src/openvpn/multi.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,12 @@ multi_ifconfig_pool_persist(struct multi_context *m, bool force)
169169
}
170170

171171
static void
172-
multi_reap_range(const struct multi_context *m, int start_bucket, int end_bucket)
172+
multi_reap_range(const struct multi_context *m, uint32_t start_bucket, uint32_t end_bucket)
173173
{
174174
struct gc_arena gc = gc_new();
175175
struct hash_iterator hi;
176176
struct hash_element *he;
177177

178-
if (start_bucket < 0)
179-
{
180-
start_bucket = 0;
181-
end_bucket = hash_n_buckets(m->vhash);
182-
}
183-
184178
dmsg(D_MULTI_DEBUG, "MULTI: REAP range %d -> %d", start_bucket, end_bucket);
185179
hash_iterator_init_range(m->vhash, &hi, start_bucket, end_bucket);
186180
while ((he = hash_iterator_next(&hi)) != NULL)
@@ -201,11 +195,11 @@ multi_reap_range(const struct multi_context *m, int start_bucket, int end_bucket
201195
static void
202196
multi_reap_all(const struct multi_context *m)
203197
{
204-
multi_reap_range(m, -1, 0);
198+
multi_reap_range(m, 0, hash_n_buckets(m->vhash));
205199
}
206200

207201
static struct multi_reap *
208-
multi_reap_new(int buckets_per_pass)
202+
multi_reap_new(uint32_t buckets_per_pass)
209203
{
210204
struct multi_reap *mr;
211205
ALLOC_OBJ(mr, struct multi_reap);
@@ -237,10 +231,10 @@ multi_reap_free(struct multi_reap *mr)
237231
/*
238232
* How many buckets in vhash to reap per pass.
239233
*/
240-
static int
241-
reap_buckets_per_pass(int n_buckets)
234+
static uint32_t
235+
reap_buckets_per_pass(uint32_t n_buckets)
242236
{
243-
return constrain_int(n_buckets / REAP_DIVISOR, REAP_MIN, REAP_MAX);
237+
return constrain_uint(n_buckets / REAP_DIVISOR, REAP_MIN, REAP_MAX);
244238
}
245239

246240
#ifdef ENABLE_MANAGEMENT

src/openvpn/multi.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151
*/
5252
struct multi_reap
5353
{
54-
int bucket_base;
55-
int buckets_per_pass;
54+
uint32_t bucket_base;
55+
uint32_t buckets_per_pass;
5656
time_t last_call;
5757
};
5858

src/openvpn/options.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7956,8 +7956,8 @@ add_option(struct options *options, char *p[], bool is_inline, const char *file,
79567956
{
79577957
goto err;
79587958
}
7959-
options->real_hash_size = real;
7960-
options->virtual_hash_size = virtual;
7959+
options->real_hash_size = (uint32_t)real;
7960+
options->virtual_hash_size = (uint32_t)virtual;
79617961
}
79627962
else if (streq(p[0], "connect-freq") && p[1] && p[2] && !p[3])
79637963
{

src/openvpn/options.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,8 @@ struct options
499499
struct in6_addr ifconfig_ipv6_pool_base; /* IPv6 */
500500
int ifconfig_ipv6_pool_netbits; /* IPv6 */
501501

502-
int real_hash_size;
503-
int virtual_hash_size;
502+
uint32_t real_hash_size;
503+
uint32_t virtual_hash_size;
504504
const char *client_connect_script;
505505
const char *client_disconnect_script;
506506
const char *learn_address_script;

tests/unit_tests/openvpn/test_misc.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ static uint32_t
128128
word_hash_function(const void *key, uint32_t iv)
129129
{
130130
const char *str = (const char *)key;
131-
const int len = strlen(str);
131+
const uint32_t len = (uint32_t)strlen(str);
132132
return hash_func((const uint8_t *)str, len, iv);
133133
}
134134

@@ -138,11 +138,11 @@ word_compare_function(const void *key1, const void *key2)
138138
return strcmp((const char *)key1, (const char *)key2) == 0;
139139
}
140140

141-
static unsigned long
141+
static uint32_t
142142
get_random(void)
143143
{
144144
/* rand() is not very random, but it's C99 and this is just for testing */
145-
return rand();
145+
return (uint32_t)rand();
146146
}
147147

148148
static struct hash_element *
@@ -176,7 +176,7 @@ test_list(void **state)
176176
struct hash *hash = hash_init(10000, get_random(), word_hash_function, word_compare_function);
177177
struct hash *nhash = hash_init(256, get_random(), word_hash_function, word_compare_function);
178178

179-
printf("hash_init n_buckets=%d mask=0x%08x\n", hash->n_buckets, hash->mask);
179+
printf("hash_init n_buckets=%u mask=0x%08x\n", hash->n_buckets, hash->mask);
180180

181181
char wordfile[PATH_MAX] = { 0 };
182182
openvpn_test_get_srcdir_dir(wordfile, PATH_MAX, "/../../../COPYRIGHT.GPL");
@@ -254,10 +254,10 @@ test_list(void **state)
254254

255255
/* output contents of hash table */
256256
{
257-
ptr_type inc = 0;
257+
uint32_t inc = 0;
258258
int count = 0;
259259

260-
for (ptr_type base = 0; base < hash_n_buckets(hash); base += inc)
260+
for (uint32_t base = 0; base < hash_n_buckets(hash); base += inc)
261261
{
262262
struct hash_iterator hi;
263263
struct hash_element *he;

0 commit comments

Comments
 (0)