]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
lru: fix case when inserting value with larger size than allocated
authorMarek Vavruša <mvavrusa@cloudflare.com>
Wed, 16 May 2018 04:34:00 +0000 (21:34 -0700)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Wed, 21 Nov 2018 13:26:59 +0000 (14:26 +0100)
This fixes a case when inserting into LRU, and the entry for given
key exists, but has allocated smaller value than what's requested.

lib/generic/lru.c

index 7ab31ae9b8e082b550d00ff32e260f0430b7a38f..12bba4ed728954f69892e704be2f1ca2577c5baa 100644 (file)
@@ -165,8 +165,15 @@ KR_EXPORT void * lru_get_impl(struct lru *lru, const char *key, uint key_len,
                if (g->hashes[i] == khash_top) {
                        it = g->items[i];
                        if (likely(it && it->key_len == key_len
-                                       && (key_len == 0 || memcmp(it->data, key, key_len) == 0)))
-                               goto found; // to reduce huge nesting depth
+                                       && (key_len == 0 || memcmp(it->data, key, key_len) == 0))) {
+                               /* Found a key, but trying to insert a value larger than available
+                                * space in the allocated slot, so the entry must be resized to fit. */
+                               if (unlikely(do_insert && val_len > it->val_len)) {
+                                       goto insert;
+                               } else {
+                                       goto found; // to reduce huge nesting depth     
+                               }
+                       }
                }
        }
        // key not found; first try an empty/counted-out place to insert