From: Vladimír Čunát Date: Wed, 31 Aug 2016 14:35:54 +0000 (+0200) Subject: lru_get_new *can* return NULL X-Git-Tag: v1.2.0-rc1~88^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=eeec66a1908b75505b12a9bf206027e9de8d1325;p=thirdparty%2Fknot-resolver.git lru_get_new *can* return NULL ... and that doesn't necessarily mean that malloc() failed. We do *not* want to evict a heavy-hitter by an unfrequent element. Note: even the implementation currently in master *did* return NULL, so some parts of the code were just wrongly returning ENOMEM. --- diff --git a/lib/cookies/lru_cache.c b/lib/cookies/lru_cache.c index feff44ff3..52396f468 100644 --- a/lib/cookies/lru_cache.c +++ b/lib/cookies/lru_cache.c @@ -62,11 +62,9 @@ int kr_cookie_lru_set(kr_cookie_lru_t *cache, const struct sockaddr *sa, } struct cookie_opt_data *cached = lru_get_new(cache, addr, addr_len); - if (!cached) { - return kr_error(ENOMEM); + if (cached) { + memcpy(cached->opt_data, opt, opt_size); } - memcpy(cached->opt_data, opt, opt_size); - return kr_ok(); } diff --git a/lib/nsrep.c b/lib/nsrep.c index cc737f235..00e05612f 100644 --- a/lib/nsrep.c +++ b/lib/nsrep.c @@ -260,7 +260,7 @@ int kr_nsrep_update_rtt(struct kr_nsrep *ns, const struct sockaddr *addr, } unsigned *cur = lru_get_new(cache, addr_in, addr_len); if (!cur) { - return kr_error(ENOMEM); + return kr_ok(); } /* Score limits */ if (score > KR_NS_MAX_SCORE) { @@ -294,9 +294,8 @@ int kr_nsrep_update_rep(struct kr_nsrep *ns, unsigned reputation, kr_nsrep_lru_t ns->reputation = reputation; /* Store reputation in the LRU cache */ unsigned *cur = lru_get_new(cache, (const char *)ns->name, knot_dname_size(ns->name)); - if (!cur) { - return kr_error(ENOMEM); + if (cur) { + *cur = reputation; } - *cur = reputation; return kr_ok(); } diff --git a/tests/test_lru.c b/tests/test_lru.c index ff8c76496..f6ce2a3e5 100644 --- a/tests/test_lru.c +++ b/tests/test_lru.c @@ -62,7 +62,9 @@ static void test_insert(void **state) for (i = 0; i < dict_size; i++) { int *data = lru_get_new(lru, dict[i], KEY_LEN(dict[i])); - assert_non_null(data); + if (!data) { + continue; + } *data = i; assert_true(*lru_get_try(lru, dict[i], KEY_LEN(dict[i])) == i); } @@ -83,7 +85,7 @@ static void test_eviction(void **state) test_randstr(key, sizeof(key)); int *data = lru_get_new(lru, key, sizeof(key)); if (!data) { - assert_true(0); + continue; } *data = i; if (*lru_get_try(lru, key, sizeof(key)) != i) {