From: Tomas Krizek Date: Wed, 24 Mar 2021 16:44:47 +0000 (+0100) Subject: lib/utils.c: replace asserts X-Git-Tag: v5.4.0~18^2~27 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=af54c5f5280ad744b37f14557989dff639dd2236;p=thirdparty%2Fknot-resolver.git lib/utils.c: replace asserts --- diff --git a/lib/utils.c b/lib/utils.c index bff6ed26c..f61ec40fd 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2014-2017 CZ.NIC, z.s.p.o. +/* Copyright (C) 2014-2021 CZ.NIC, z.s.p.o. * SPDX-License-Identifier: GPL-3.0-or-later */ @@ -187,7 +187,10 @@ char* kr_strcatdup(unsigned n, ...) char * kr_absolutize_path(const char *dirname, const char *fname) { - assert(dirname && fname); + if (!kr_assume(dirname && fname)) { + errno = EINVAL; + return NULL; + } char *result; int aret; if (dirname[0] == '/') { // absolute path is easier @@ -241,7 +244,7 @@ static int pkt_recycle(knot_pkt_t *pkt, bool keep_question) if (keep_question) { base_size += knot_pkt_question_size(pkt); } - assert(base_size <= sizeof(buf)); + if (!kr_assume(base_size <= sizeof(buf))) return kr_error(EINVAL); memcpy(buf, pkt->wire, base_size); /* Clear the packet and its auxiliary structures */ @@ -292,7 +295,7 @@ int kr_pkt_put(knot_pkt_t *pkt, const knot_dname_t *name, uint32_t ttl, void kr_pkt_make_auth_header(knot_pkt_t *pkt) { - assert(pkt && pkt->wire); + if (!kr_assume(pkt && pkt->wire)) return; knot_wire_clear_ad(pkt->wire); knot_wire_set_aa(pkt->wire); } @@ -481,7 +484,7 @@ struct sockaddr * kr_straddr_socket(const char *addr, int port, knot_mm_t *pool) return (struct sockaddr *)res; } default: - assert(!EINVAL); + (void)!kr_assume(false); return NULL; } } @@ -521,7 +524,7 @@ int kr_straddr_subnet(void *dst, const char *addr) int kr_straddr_split(const char *instr, char ipaddr[static restrict (INET6_ADDRSTRLEN + 1)], uint16_t *port) { - assert(instr && ipaddr && port); + if (!kr_assume(instr && ipaddr && port)) return kr_error(EINVAL); /* Find where port number starts. */ const char *p_start = strchr(instr, '@'); if (!p_start) @@ -585,7 +588,7 @@ int kr_bitcmp(const char *a, const char *b, int bits) return 1; } - assert((a && b && bits >= 0) || bits == 0); + kr_require((a && b && bits >= 0) || bits == 0); /* Compare part byte-divisible part. */ const size_t chunk = bits / 8; int ret = memcmp(a, b, chunk); @@ -653,11 +656,7 @@ static inline bool rrsets_match(const knot_rrset_t *rr1, const knot_rrset_t *rr2 */ static int to_wire_ensure_unique(ranked_rr_array_t *array, size_t index) { - bool ok = array && index < array->len; - if (!ok) { - assert(false); - return kr_error(EINVAL); - } + if (!kr_assume(array && index < array->len)) return kr_error(EINVAL); const struct ranked_rr_array_entry *e0 = array->at[index]; if (!e0->to_wire) { @@ -706,12 +705,9 @@ int kr_ranked_rrarray_add(ranked_rr_array_t *array, const knot_rrset_t *rr, continue; } /* Found the entry to merge with. Check consistency and merge. */ - bool ok = stashed->rank == rank && !stashed->cached && stashed->in_progress; - if (!ok) { - assert(false); + if (!kr_assume(stashed->rank == rank && !stashed->cached && stashed->in_progress)) return kr_error(EEXIST); - } - /* assert(rr->rrs.count == 1); */ + //(void)!kr_assume(rr->rrs.count == 1); /* ^^ shouldn't be a problem for this function, but it's probably a bug */ /* It may happen that an RRset is first considered useful @@ -736,9 +732,7 @@ int kr_ranked_rrarray_add(ranked_rr_array_t *array, const knot_rrset_t *rr, knot_rdata_t *r_it = stashed->rr->rrs.rdata; for (int ri = 0; ri < stashed->rr->rrs.count; ++ri, r_it = knot_rdataset_next(r_it)) { - if (array_push(*ra, r_it) < 0) { - abort(); - } + kr_require(array_push(*ra, r_it) >= 0); } } else { int ret = array_reserve_mm(*ra, ra->len + rr->rrs.count, @@ -751,9 +745,7 @@ int kr_ranked_rrarray_add(ranked_rr_array_t *array, const knot_rrset_t *rr, knot_rdata_t *r_it = rr->rrs.rdata; for (int ri = 0; ri < rr->rrs.count; ++ri, r_it = knot_rdataset_next(r_it)) { - if (array_push(*ra, r_it) < 0) { - abort(); - } + kr_require(array_push(*ra, r_it) >= 0); } return i; } @@ -775,7 +767,7 @@ int kr_ranked_rrarray_add(ranked_rr_array_t *array, const knot_rrset_t *rr, return kr_error(ENOMEM); } rr_new->rrs = rr->rrs; - assert(rr_new->additional == NULL); + if (!kr_assume(rr_new->additional == NULL)) return kr_error(EINVAL); entry->qry_uid = qry_uid; entry->rr = rr_new; @@ -861,7 +853,8 @@ int kr_ranked_rrarray_finalize(ranked_rr_array_t *array, uint32_t qry_uid, knot_ raw_it += size; } } - assert(raw_it == (uint8_t *)rds->rdata + rds->size); + if (!kr_assume(raw_it == (uint8_t *)rds->rdata + rds->size)) + return kr_error(EINVAL); } stashed->in_progress = false; } @@ -1103,10 +1096,7 @@ void kr_uv_free_cb(uv_handle_t* handle) const char *kr_strptime_diff(const char *format, const char *time1_str, const char *time0_str, double *diff) { - assert(format != NULL); - assert(time1_str != NULL); - assert(time0_str != NULL); - assert(diff != NULL); + if (!kr_assume(format && time1_str && time0_str && diff)) return NULL; struct tm time1_tm; time_t time1_u; @@ -1136,11 +1126,7 @@ const char *kr_strptime_diff(const char *format, const char *time1_str, int knot_dname_lf2wire(knot_dname_t * const dst, uint8_t len, const uint8_t *lf) { knot_dname_t *d = dst; /* moving "cursor" as we write it out */ - bool ok = d && (len == 0 || lf); - if (!ok) { - assert(false); - return kr_error(EINVAL); - } + if (!kr_assume(d && (len == 0 || lf))) return kr_error(EINVAL); /* we allow the final zero byte to be omitted */ if (!len) { goto finish; @@ -1157,7 +1143,7 @@ int knot_dname_lf2wire(knot_dname_t * const dst, uint8_t len, const uint8_t *lf) --i; const int label_start = i + 1; /* index of the first byte of the current label */ const int label_len = label_end - label_start; - assert(label_len >= 0); + (void)!kr_assume(label_len >= 0); if (label_len > 63 || label_len <= 0) return kr_error(EILSEQ); /* write the label */ @@ -1211,7 +1197,7 @@ void kr_rnd_buffered(void *data, uint size) void kr_rrset_init(knot_rrset_t *rrset, knot_dname_t *owner, uint16_t type, uint16_t rclass, uint32_t ttl) { - assert(rrset); + if (!kr_assume(rrset)) return; knot_rrset_init(rrset, owner, type, rclass, ttl); } uint16_t kr_pkt_has_dnssec(const knot_pkt_t *pkt)