mirror of
https://git.proxmox.com/git/mirror_frr
synced 2025-04-29 18:10:23 +00:00
lib: typesafe hash table breadcrumbs
Looking at the coverity report, it complains that tabshift could be zero, resulting in a uint32_t shifted by 33 (which is undefined.) As I was confused by the "+ 1", in addition to the SA assume(), leave some breadcumbs for next time this comes up. Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
This commit is contained in:
parent
4dbef8567b
commit
ae19023b8e
@ -85,6 +85,15 @@ void typesafe_hash_grow(struct thash_head *head)
|
|||||||
uint32_t newsize = head->count, i, j;
|
uint32_t newsize = head->count, i, j;
|
||||||
uint8_t newshift, delta;
|
uint8_t newshift, delta;
|
||||||
|
|
||||||
|
/* note hash_grow is called after head->count++, so newsize is
|
||||||
|
* guaranteed to be >= 1. So the minimum argument to builtin_ctz
|
||||||
|
* below is 2, which returns 1, and that makes newshift >= 2.
|
||||||
|
*
|
||||||
|
* Calling hash_grow with a zero head->count would result in a
|
||||||
|
* malformed hash table that has tabshift == 1.
|
||||||
|
*/
|
||||||
|
assert(head->count > 0);
|
||||||
|
|
||||||
hash_consistency_check(head);
|
hash_consistency_check(head);
|
||||||
|
|
||||||
newsize |= newsize >> 1;
|
newsize |= newsize >> 1;
|
||||||
|
@ -783,6 +783,12 @@ struct thash_head {
|
|||||||
struct thash_item **entries;
|
struct thash_item **entries;
|
||||||
uint32_t count;
|
uint32_t count;
|
||||||
|
|
||||||
|
/* tabshift can be 0 if the hash table is empty and entries is NULL.
|
||||||
|
* otherwise it will always be 2 or larger because it contains
|
||||||
|
* the shift value *plus 1*. This is a trick to make HASH_SIZE return
|
||||||
|
* the correct value (with the >> 1) for tabshift == 0, without needing
|
||||||
|
* a conditional branch.
|
||||||
|
*/
|
||||||
uint8_t tabshift;
|
uint8_t tabshift;
|
||||||
uint8_t minshift, maxshift;
|
uint8_t minshift, maxshift;
|
||||||
};
|
};
|
||||||
@ -792,7 +798,10 @@ struct thash_head {
|
|||||||
#define HASH_SIZE(head) \
|
#define HASH_SIZE(head) \
|
||||||
_HASH_SIZE((head).tabshift)
|
_HASH_SIZE((head).tabshift)
|
||||||
#define _HASH_KEY(tabshift, val) \
|
#define _HASH_KEY(tabshift, val) \
|
||||||
((val) >> (33 - (tabshift)))
|
({ \
|
||||||
|
assume((tabshift) >= 2 && (tabshift) <= 33); \
|
||||||
|
(val) >> (33 - (tabshift)); \
|
||||||
|
})
|
||||||
#define HASH_KEY(head, val) \
|
#define HASH_KEY(head, val) \
|
||||||
_HASH_KEY((head).tabshift, val)
|
_HASH_KEY((head).tabshift, val)
|
||||||
#define HASH_GROW_THRESHOLD(head) \
|
#define HASH_GROW_THRESHOLD(head) \
|
||||||
|
Loading…
Reference in New Issue
Block a user