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-Tag: v3.2.0~29^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=695d3895fe148965f7d1fe05c3d64f73849e9ab9;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 7ab31ae9b..12bba4ed7 100644 --- a/lib/generic/lru.c +++ b/lib/generic/lru.c @@ -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