aboutsummaryrefslogtreecommitdiffstats
path: root/src/libutil/hash.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2017-05-13 09:58:07 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2017-05-13 09:58:07 +0100
commit766af6da323a191f34e6b66edadf2032d2aac1a1 (patch)
tree4ff1a4f7d445b1c7f41fbc88c8eefa41f8937a81 /src/libutil/hash.c
parent31d00c13a6bb071b92a03e939069998a837d5f0a (diff)
downloadrspamd-766af6da323a191f34e6b66edadf2032d2aac1a1.tar.gz
rspamd-766af6da323a191f34e6b66edadf2032d2aac1a1.zip
[Minor] Add more sanity guards for evictions
Diffstat (limited to 'src/libutil/hash.c')
-rw-r--r--src/libutil/hash.c32
1 files changed, 22 insertions, 10 deletions
diff --git a/src/libutil/hash.c b/src/libutil/hash.c
index 1de90b605..1f4fdc22b 100644
--- a/src/libutil/hash.c
+++ b/src/libutil/hash.c
@@ -71,24 +71,33 @@ rspamd_lru_hash_remove_evicted (rspamd_lru_hash_t *hash,
{
guint i;
- g_assert (elt->eviction_pos < eviction_candidates);
+ g_assert (hash->eviction_used > 0);
+ g_assert (elt->eviction_pos < hash->eviction_used);
memmove (&hash->eviction_pool[elt->eviction_pos],
&hash->eviction_pool[elt->eviction_pos + 1],
sizeof (rspamd_lru_element_t *) *
(eviction_candidates - elt->eviction_pos - 1));
- hash->eviction_used --;
- if (elt->lg_usages <= hash->eviction_min_prio) {
- /* We also need to update min_prio */
- hash->eviction_min_prio = G_MAXUINT;
+ hash->eviction_used--;
- for (i = 0; i < hash->eviction_used; i ++) {
- if (hash->eviction_min_prio > hash->eviction_pool[i]->lg_usages) {
- hash->eviction_min_prio = hash->eviction_pool[i]->lg_usages;
+ if (hash->eviction_used > 0) {
+ if (elt->lg_usages <= hash->eviction_min_prio) {
+ /* We also need to update min_prio */
+ hash->eviction_min_prio = G_MAXUINT;
+
+ for (i = 0; i < hash->eviction_used; i ++) {
+ if (hash->eviction_min_prio > hash->eviction_pool[i]->lg_usages) {
+ hash->eviction_min_prio = hash->eviction_pool[i]->lg_usages;
+ }
}
}
}
+ else {
+ hash->eviction_min_prio = G_MAXUINT;
+ }
+
+
}
static void
@@ -143,7 +152,7 @@ rspamd_lru_hash_maybe_evict (rspamd_lru_hash_t *hash,
return TRUE;
}
- else if (hash->eviction_min_prio > elt->lg_usages) {
+ else {
/* Find any candidate that has higher usage count */
for (i = 0; i < hash->eviction_used; i ++) {
cur = hash->eviction_pool[i];
@@ -152,7 +161,10 @@ rspamd_lru_hash_maybe_evict (rspamd_lru_hash_t *hash,
cur->eviction_pos = -1;
elt->eviction_pos = i;
hash->eviction_pool[i] = elt;
- hash->eviction_min_prio = elt->lg_usages;
+
+ if (hash->eviction_min_prio > elt->lg_usages) {
+ hash->eviction_min_prio = elt->lg_usages;
+ }
return TRUE;
}