]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
lib/utils.c: replace asserts
authorTomas Krizek <tomas.krizek@nic.cz>
Wed, 24 Mar 2021 16:44:47 +0000 (17:44 +0100)
committerTomas Krizek <tomas.krizek@nic.cz>
Tue, 25 May 2021 12:39:42 +0000 (14:39 +0200)
lib/utils.c

index bff6ed26cafe51ec30cb8eb14d8dc04e08af333c..f61ec40fd7a60649b7784229e4253c9bbebe81f7 100644 (file)
@@ -1,4 +1,4 @@
-/*  Copyright (C) 2014-2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
+/*  Copyright (C) 2014-2021 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
  *  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)