From: Marek VavruĊĦa Date: Wed, 16 May 2018 04:34:00 +0000 (-0700) Subject: lru: fix case when inserting value with larger size than allocated X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0ae322f4ab91cd38683ffba590d9cd47bbdbb043;p=thirdparty%2Fknot-resolver.git lru: fix case when inserting value with larger size than allocated This fixes a case when inserting into LRU, and the entry for given key exists, but has allocated smaller value than what's requested. --- diff --git a/lib/generic/lru.c b/lib/generic/lru.c index c04f6f09f..b9b6a0005 100644 --- a/lib/generic/lru.c +++ b/lib/generic/lru.c @@ -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