Commit 294d0f19 authored by Marek Vavruša's avatar Marek Vavruša

lib: cleanup, return value checks

parent d3db7199
......@@ -84,8 +84,10 @@ void udp_recv(uv_udp_t *handle, ssize_t nread, const uv_buf_t *buf,
}
knot_pkt_t *query = knot_pkt_new(buf->base, nread, &worker->pkt_pool);
query->max_size = KNOT_WIRE_MAX_PKTSIZE;
worker_exec(worker, (uv_handle_t *)handle, query, addr);
if (query) {
query->max_size = KNOT_WIRE_MAX_PKTSIZE;
worker_exec(worker, (uv_handle_t *)handle, query, addr);
}
mp_flush(worker->pkt_pool.ctx);
}
......
......@@ -564,8 +564,11 @@ static int qr_task_step(struct qr_task *task, const struct sockaddr *packet_sour
return kr_ok();
}
static int parse_query(knot_pkt_t *query)
static int parse_packet(knot_pkt_t *query)
{
if (!query)
return kr_error(EINVAL);
/* Parse query packet. */
int ret = knot_pkt_parse(query, 0);
if (ret != KNOT_EOK) {
......@@ -582,19 +585,19 @@ static int parse_query(knot_pkt_t *query)
int worker_exec(struct worker_ctx *worker, uv_handle_t *handle, knot_pkt_t *query, const struct sockaddr* addr)
{
if (!worker) {
if (!worker || !handle) {
return kr_error(EINVAL);
}
/* Parse query */
int ret = parse_query(query);
/* Parse packet */
int ret = parse_packet(query);
/* Start new task on master sockets, or resume existing */
struct qr_task *task = handle->data;
bool is_master_socket = (!task);
if (is_master_socket) {
/* Ignore badly formed queries or responses. */
if (ret != 0 || knot_wire_get_qr(query->wire)) {
if (!query || ret != 0 || knot_wire_get_qr(query->wire)) {
DEBUG_MSG("task bad_query %p => %d, %s\n", task, ret, kr_strerror(ret));
worker->stats.dropped += 1;
return kr_error(EINVAL); /* Ignore. */
......@@ -687,8 +690,8 @@ int worker_reserve(struct worker_ctx *worker, size_t ring_maxlen)
{
array_init(worker->pools);
array_init(worker->ioreqs);
array_reserve(worker->pools, ring_maxlen);
array_reserve(worker->ioreqs, ring_maxlen);
if (array_reserve(worker->pools, ring_maxlen) || array_reserve(worker->ioreqs, ring_maxlen))
return kr_error(ENOMEM);
memset(&worker->pkt_pool, 0, sizeof(worker->pkt_pool));
worker->pkt_pool.ctx = mp_new (4 * sizeof(knot_pkt_t));
worker->pkt_pool.alloc = (mm_alloc_t) mp_alloc;
......
......@@ -222,9 +222,10 @@ int kr_cache_peek(struct kr_cache_txn *txn, uint8_t tag, const knot_dname_t *nam
static void entry_write(struct kr_cache_entry *dst, struct kr_cache_entry *header, namedb_val_t data)
{
assert(dst);
assert(dst && header);
memcpy(dst, header, sizeof(*header));
memcpy(dst->data, data.data, data.len);
if (data.data)
memcpy(dst->data, data.data, data.len);
}
int kr_cache_insert(struct kr_cache_txn *txn, uint8_t tag, const knot_dname_t *name, uint16_t type,
......@@ -405,6 +406,7 @@ int kr_cache_peek_rrsig(struct kr_cache_txn *txn, knot_rrset_t *rr, uint16_t *ra
if (ret != 0) {
return ret;
}
assert(entry);
if (rank) {
*rank = entry->rank;
}
......
......@@ -116,7 +116,9 @@ static int name_error_response_check_rr(int *flags, const knot_rrset_t *nsec,
/* Try to find parent wildcard that is proved by this NSEC. */
uint8_t namebuf[KNOT_DNAME_MAXLEN];
knot_dname_to_wire(namebuf, name, sizeof(namebuf));
int ret = knot_dname_to_wire(namebuf, name, sizeof(namebuf));
if (ret != 0)
return ret;
knot_dname_t *ptr = namebuf;
while (ptr[0]) {
/* Remove leftmost label and replace it with '\1*'. */
......@@ -143,14 +145,13 @@ int kr_nsec_name_error_response_check(const knot_pkt_t *pkt, knot_section_t sect
return kr_error(EINVAL);
}
int ret = kr_error(ENOENT);
int flags = 0;
for (unsigned i = 0; i < sec->count; ++i) {
const knot_rrset_t *rrset = knot_pkt_rr(sec, i);
if (rrset->type != KNOT_RRTYPE_NSEC) {
continue;
}
ret = name_error_response_check_rr(&flags, rrset, sname);
int ret = name_error_response_check_rr(&flags, rrset, sname);
if (ret != 0) {
return ret;
}
......@@ -260,7 +261,6 @@ int kr_nsec_no_data_response_check(const knot_pkt_t *pkt, knot_section_t section
return kr_error(EINVAL);
}
int ret = kr_error(ENOENT);
int flags = 0;
for (unsigned i = 0; i < sec->count; ++i) {
const knot_rrset_t *rrset = knot_pkt_rr(sec, i);
......@@ -268,7 +268,7 @@ int kr_nsec_no_data_response_check(const knot_pkt_t *pkt, knot_section_t section
continue;
}
if (knot_dname_is_equal(rrset->owner, sname)) {
ret = no_data_response_check_rrtype(&flags, rrset, stype);
int ret = no_data_response_check_rrtype(&flags, rrset, stype);
if (ret != 0) {
return ret;
}
......
......@@ -385,7 +385,6 @@ static int closest_encloser_proof(const knot_pkt_t *pkt, knot_section_t section_
const knot_rrset_t *matching = NULL;
const knot_rrset_t *covering = NULL;
int ret = kr_error(ENOENT);
int flags = 0;
const knot_dname_t *next_closer = NULL;
for (unsigned i = 0; i < sec->count; ++i) {
......@@ -395,7 +394,7 @@ static int closest_encloser_proof(const knot_pkt_t *pkt, knot_section_t section_
}
unsigned skipped = 0;
flags = 0;
ret = closest_encloser_match(&flags, rrset, sname, &skipped);
int ret = closest_encloser_match(&flags, rrset, sname, &skipped);
if (ret != 0) {
return ret;
}
......
......@@ -96,7 +96,8 @@ typedef array_t(uint8_t) pack_t;
static inline pack_objlen_t pack_obj_len(uint8_t *it)
{
pack_objlen_t len = 0;
memcpy(&len, it, sizeof(len));
if (it)
memcpy(&len, it, sizeof(len));
return len;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment