]> 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)
committerMarek Vavruša <mvavrusa@cloudflare.com>
Fri, 7 Sep 2018 17:45:21 +0000 (10:45 -0700)
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 c04f6f09fc49715fdbbe3fb9a2feb5407f191e1e..b9b6a0005e326dcc863ac92c09c60768e3390f07 100644 (file)
@@ -157,8 +157,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