-/* 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
*/
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
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 */
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);
}
return (struct sockaddr *)res;
}
default:
- assert(!EINVAL);
+ (void)!kr_assume(false);
return NULL;
}
}
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)
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);
*/
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) {
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
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,
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;
}
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;
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;
}
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;
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;
--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 */
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)