From 78eb305975ed68d8bc159e46e6164afff1a74747 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sat, 2 Nov 2024 21:41:32 +0100 Subject: [PATCH] Fix unlikely (for libevent) UB in HT_GROW() The reason it is not possible for libevent is that: a) it is unlikely to have 1610612741 elements b) growing is done incrementally (i.e. only internally by HT_INSERT) and in this case the UB is not possible Fixes: https://github.com/libevent/libevent/issues/1312 --- ht-internal.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ht-internal.h b/ht-internal.h index 50375bba..de7cd0ea 100644 --- a/ht-internal.h +++ b/ht-internal.h @@ -309,7 +309,7 @@ ht_string_hash_(const char *s) 805306457, 1610612741 \ }; \ static unsigned name##_N_PRIMES = \ - (unsigned)(sizeof(name##_PRIMES)/sizeof(name##_PRIMES[0])); \ + (unsigned)(sizeof(name##_PRIMES)/sizeof(name##_PRIMES[0])) - 1; \ /* Expand the internal table of 'head' until it is large enough to \ * hold 'size' elements. Return 0 on success, -1 on allocation \ * failure. */ \ @@ -319,7 +319,7 @@ ht_string_hash_(const char *s) unsigned new_len, new_load_limit; \ int prime_idx; \ struct type **new_table; \ - if (head->hth_prime_idx == (int)name##_N_PRIMES - 1) \ + if (head->hth_prime_idx == (int)name##_N_PRIMES) \ return 0; \ if (head->hth_load_limit > size) \ return 0; \