]> source.dussan.org Git - rspamd.git/commitdiff
[Fix] Add more guards for heap
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 11 Apr 2016 12:51:08 +0000 (13:51 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 11 Apr 2016 12:51:08 +0000 (13:51 +0100)
src/libutil/hash.c
src/libutil/heap.c

index 30ecdd85b7c3c82a35be771e2212a113db70024e..ddf916c20486be8b556334e8d88466d76cd4fe86 100644 (file)
@@ -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;
+                               }
                        }
                }
        }
index 47c9967325aecac5f39e1e18f7bafa27696bd265..6fc1d97833023a63828202a22176470149bb74ae 100644 (file)
@@ -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;
 }