From 38bced7829e676d43264ab1162034a6c3ad3f91d Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 11 Apr 2016 13:51:08 +0100 Subject: [PATCH] [Fix] Add more guards for heap --- src/libutil/hash.c | 10 +++++++++- src/libutil/heap.c | 15 +++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/libutil/hash.c b/src/libutil/hash.c index 30ecdd85b..ddf916c20 100644 --- a/src/libutil/hash.c +++ b/src/libutil/hash.c @@ -146,7 +146,9 @@ rspamd_lru_hash_insert (rspamd_lru_hash_t *hash, gpointer key, gpointer value, guint i; res = g_hash_table_lookup (hash->tbl, key); + if (res != NULL) { + rspamd_min_heap_remove_elt (hash->heap, &res->helt); g_hash_table_remove (hash->tbl, key); } else { @@ -155,7 +157,13 @@ rspamd_lru_hash_insert (rspamd_lru_hash_t *hash, gpointer key, gpointer value, for (i = 0; i < MIN (hash->maxsize, expire_aggressive_count); i ++) { res = (rspamd_lru_element_t *)rspamd_min_heap_pop (hash->heap); - g_hash_table_remove (hash->tbl, res->key); + + if (res) { + g_hash_table_remove (hash->tbl, res->key); + } + else { + break; + } } } } diff --git a/src/libutil/heap.c b/src/libutil/heap.c index 47c996732..6fc1d9783 100644 --- a/src/libutil/heap.c +++ b/src/libutil/heap.c @@ -123,10 +123,17 @@ rspamd_min_heap_pop (struct rspamd_min_heap *heap) elt = g_ptr_array_index (heap->ar, 0); last = g_ptr_array_index (heap->ar, heap->ar->len - 1); - /* Now replace elt with the last element and sink it if needed */ - heap_swap (heap, elt, last); - g_ptr_array_remove_index_fast (heap->ar, heap->ar->len - 1); - rspamd_min_heap_sink (heap, last); + + if (elt != last) { + /* Now replace elt with the last element and sink it if needed */ + heap_swap (heap, elt, last); + g_ptr_array_remove_index_fast (heap->ar, heap->ar->len - 1); + rspamd_min_heap_sink (heap, last); + } + else { + g_ptr_array_remove_index_fast (heap->ar, heap->ar->len - 1); + } + return elt; } -- 2.39.5