]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
lru_get_new *can* return NULL
authorVladimír Čunát <vladimir.cunat@nic.cz>
Wed, 31 Aug 2016 14:35:54 +0000 (16:35 +0200)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Wed, 2 Nov 2016 10:14:05 +0000 (11:14 +0100)
... 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.

lib/cookies/lru_cache.c
lib/nsrep.c
tests/test_lru.c

index feff44ff3e82a5dca6d8f19ef5fec25319a1da8a..52396f468199ee6427bdba46b3155c81c89947e1 100644 (file)
@@ -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();
 }
index cc737f235ec29b6dbf021b8aca943cb044d21f2a..00e05612f647245181437753158892a9aeb9d36a 100644 (file)
@@ -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();
 }
index ff8c76496b546feda7f0c6e7835eb3c40e2564eb..f6ce2a3e5ea49d33e14429469591ddd1e74dd137 100644 (file)
@@ -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) {