From: Tomas Krizek Date: Wed, 24 Mar 2021 16:44:47 +0000 (+0100) Subject: lib/zonecut.c: replace asserts X-Git-Tag: v5.4.0~18^2~25 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=746b1e25c77ce1d6071024a130352ea0ff042603;p=thirdparty%2Fknot-resolver.git lib/zonecut.c: replace asserts --- diff --git a/lib/zonecut.c b/lib/zonecut.c index cf7bfea46..cb879edec 100644 --- a/lib/zonecut.c +++ b/lib/zonecut.c @@ -57,9 +57,8 @@ int kr_zonecut_init(struct kr_zonecut *cut, const knot_dname_t *name, knot_mm_t /** Completely free a pack_t. */ static inline void free_addr_set(pack_t *pack, knot_mm_t *pool) { - if (unlikely(!pack)) { + if (!kr_assume(pack)) { /* promised we don't store NULL packs */ - assert(false); return; } pack_clear_mm(*pack, mm_free, pool); @@ -164,14 +163,12 @@ int kr_zonecut_copy_trust(struct kr_zonecut *dst, const struct kr_zonecut *src) int kr_zonecut_add(struct kr_zonecut *cut, const knot_dname_t *ns, const void *data, int len) { - if (!cut || !ns || !cut->nsset || (data && len <= 0)) { - assert(!EINVAL); + if (!kr_assume(cut && ns && cut->nsset && (!data || len > 0))) return kr_error(EINVAL); - } /* Disabled; add_reverse_pair() misuses this for domain name in rdata. */ if (false && data && len != sizeof(struct in_addr) && len != sizeof(struct in6_addr)) { - assert(!EINVAL); + (void)!kr_assume(!EINVAL); return kr_error(EINVAL); } @@ -219,8 +216,9 @@ int kr_zonecut_del(struct kr_zonecut *cut, const knot_dname_t *ns, const void *d if (pack->len == 0) { free_addr_set(pack, cut->pool); ret = trie_del(cut->nsset, (const char *)ns, knot_dname_size(ns), NULL); - assert(ret == 0); /* only KNOT_ENOENT and that *can't* happen */ - return (ret == 0) ? kr_ok() : kr_error(ret); + if (!kr_assume(ret == 0)) /* only KNOT_ENOENT and that *can't* happen */ + return kr_error(ret); + return kr_ok(); } return ret; @@ -237,7 +235,7 @@ int kr_zonecut_del_all(struct kr_zonecut *cut, const knot_dname_t *ns) int ret = trie_del(cut->nsset, (const char *)ns, knot_dname_size(ns), (trie_val_t *)&pack); if (ret) { /* deletion failed */ - assert(ret == KNOT_ENOENT); + (void)!kr_assume(ret == KNOT_ENOENT); return kr_error(ENOENT); } free_addr_set(pack, cut->pool); @@ -263,10 +261,8 @@ static int has_address(trie_val_t *v, void *baton_) bool kr_zonecut_is_empty(struct kr_zonecut *cut) { - if (!cut || !cut->nsset) { - assert(false); + if (!kr_assume(cut && cut->nsset)) return true; - } return !trie_apply(cut->nsset, has_address, NULL); } @@ -299,7 +295,7 @@ static addrset_info_t fetch_addr(pack_t *addrs, const knot_dname_t *ns, uint16_t rdlen = 16; break; default: - assert(!EINVAL); + (void)!kr_assume(!EINVAL); return AI_UNKNOWN; } @@ -341,7 +337,7 @@ static addrset_info_t fetch_addr(pack_t *addrs, const knot_dname_t *ns, uint16_t ++usable_cnt; ret = pack_obj_push(addrs, rd->data, rd->len); - assert(!ret); /* didn't fit because of incorrectly reserved memory */ + (void)!kr_assume(!ret); /* didn't fit because of incorrectly reserved memory */ /* LATER: for now we lose quite some information here, * as keeping it would need substantial changes on other places, * and it turned out to be premature optimization (most likely). @@ -398,7 +394,7 @@ static int fetch_ns(struct kr_context *ctx, struct kr_zonecut *cut, pack_t **pack = (pack_t **)trie_get_ins(cut->nsset, (const char *)ns_name, ns_size); if (!pack) return kr_error(ENOMEM); - assert(!*pack); /* not critical, really */ + (void)!kr_assume(!*pack); /* not critical, really */ *pack = mm_alloc(cut->pool, sizeof(pack_t)); if (!*pack) return kr_error(ENOMEM); pack_init(**pack); @@ -464,29 +460,23 @@ static int fetch_secure_rrset(knot_rrset_t **rr, struct kr_cache *cache, const knot_dname_t *owner, uint16_t type, knot_mm_t *pool, const struct kr_query *qry) { - if (!rr) { - assert(!EINVAL); + if (!kr_assume(rr)) return kr_error(EINVAL); - } /* peek, check rank and TTL */ struct kr_cache_p peek; int ret = kr_cache_peek_exact(cache, owner, type, &peek); - if (ret != 0) { + if (ret != 0) return ret; - } - if (!kr_rank_test(peek.rank, KR_RANK_SECURE)) { + if (!kr_rank_test(peek.rank, KR_RANK_SECURE)) return kr_error(ENOENT); - } int32_t new_ttl = kr_cache_ttl(&peek, qry, owner, type); - if (new_ttl < 0) { + if (new_ttl < 0) return kr_error(ESTALE); - } /* materialize a new RRset */ knot_rrset_free(*rr, pool); *rr = mm_alloc(pool, sizeof(knot_rrset_t)); - if (*rr == NULL) { + if (*rr == NULL) return kr_error(ENOMEM); - } owner = knot_dname_copy(/*const-cast*/(knot_dname_t *)owner, pool); if (!owner) { mm_free(pool, *rr); @@ -509,10 +499,8 @@ int kr_zonecut_find_cached(struct kr_context *ctx, struct kr_zonecut *cut, const knot_dname_t *name, const struct kr_query *qry, bool * restrict secured) { - if (!ctx || !cut || !name) { - //assert(false); + if (!ctx || !cut || !name) return kr_error(EINVAL); - } /* I'm not sure whether the caller always passes a clean state; * mixing doesn't seem to make sense in any case, so let's clear it. * We don't bother freeing the packs, as they're on mempool. */