From: Tomas Krizek Date: Wed, 24 Mar 2021 16:44:43 +0000 (+0100) Subject: lib/cache/entry_list.c: replace asserts X-Git-Tag: v5.4.0~18^2~58 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d3f8035b63defd55f7f4bf10c7a132b624fa764d;p=thirdparty%2Fknot-resolver.git lib/cache/entry_list.c: replace asserts --- diff --git a/lib/cache/entry_list.c b/lib/cache/entry_list.c index b4c453266..a0bdf53c0 100644 --- a/lib/cache/entry_list.c +++ b/lib/cache/entry_list.c @@ -15,7 +15,8 @@ static int entry_h_len(knot_db_val_t val); void entry_list_memcpy(struct entry_apex *ea, entry_list_t list) { - assert(ea); + if (!kr_assume(ea)) + return; memset(ea, 0, offsetof(struct entry_apex, data)); ea->has_ns = list[EL_NS ].len; ea->has_cname = list[EL_CNAME ].len; @@ -38,11 +39,8 @@ void entry_list_memcpy(struct entry_apex *ea, entry_list_t list) int entry_list_parse(const knot_db_val_t val, entry_list_t list) { - const bool ok = val.data && val.len && list; - if (!ok) { - assert(!EINVAL); + if (!kr_assume(val.data && val.len && list)) return kr_error(EINVAL); - } /* Parse the apex itself (nsec parameters). */ const struct entry_apex *ea = entry_apex_consistent(val); if (!ea) { @@ -83,26 +81,25 @@ int entry_list_parse(const knot_db_val_t val, entry_list_t list) case EL_NS: has_type = ea->has_ns; break; case EL_CNAME: has_type = ea->has_cname; break; case EL_DNAME: has_type = ea->has_dname; break; - default: assert(false); return kr_error(EINVAL); /* something very bad */ + default: + (void)!kr_assume(!EINVAL); + return kr_error(EINVAL); /* something very bad */ } if (!has_type) { list[i].len = 0; continue; } - if (it >= it_bound) { - assert(!EILSEQ); + if (!kr_assume(it < it_bound)) return kr_error(EILSEQ); - } const int len = entry_h_len( (knot_db_val_t){ .data = (void *)it, .len = it_bound - it }); - if (len < 0) { - assert(false); + if (!kr_assume(len >= 0)) return kr_error(len); - } list[i].len = len; it += to_even(len); } - assert(it == it_bound); + if (!kr_assume(it == it_bound)) /* better not use it; might be "damaged" */ + return kr_error(EILSEQ); return kr_ok(); } @@ -121,10 +118,8 @@ static int entry_h_len(const knot_db_val_t val) int sets = 2; while (sets-- > 0) { d += KR_CACHE_RR_COUNT_SIZE + rdataset_dematerialized_size(d, NULL); - if (d > data_bound) { - assert(!EILSEQ); + if (!kr_assume(d <= data_bound)) return kr_error(EILSEQ); - } } } else { /* A "packet" (opaque ATM). */ uint16_t len; @@ -132,10 +127,8 @@ static int entry_h_len(const knot_db_val_t val) memcpy(&len, d, sizeof(len)); d += 2 + to_even(len); } - if (d > data_bound) { - assert(!EILSEQ); + if (!kr_assume(d <= data_bound)) return kr_error(EILSEQ); - } return d - (uint8_t *)val.data; } @@ -225,11 +218,8 @@ int entry_h_splice( const struct kr_query *qry, struct kr_cache *cache, uint32_t timestamp) { //TODO: another review, perhaps incuding the API - const bool ok = val_new_entry && val_new_entry->len > 0; - if (!ok) { - assert(!EINVAL); + if (!kr_assume(val_new_entry && val_new_entry->len > 0)) return kr_error(EINVAL); - } int i_type; switch (type) {