Skip to content
Snippets Groups Projects
Commit 7e0f626b authored by Marek Vavrusa's avatar Marek Vavrusa
Browse files

Updated qname parsing with prealloc.

parent eba56ea4
No related merge requests found
......@@ -553,6 +553,10 @@ knot_dname_t *knot_dname_parse_from_wire(const uint8_t *wire,
/* Allocate if NULL. */
if (dname == NULL) {
dname = knot_dname_new();
if (dname) {
dname->name = (uint8_t *)malloc((i + 1) * sizeof(uint8_t));
dname->labels = (uint8_t *)malloc((l + 1) * sizeof(uint8_t));
}
}
if (dname == NULL) {
......@@ -560,27 +564,16 @@ knot_dname_t *knot_dname_parse_from_wire(const uint8_t *wire,
return NULL;
}
dname->name = (uint8_t *)malloc((i + 1) * sizeof(uint8_t));
if (dname->name == NULL) {
if (dname->name == NULL || dname->labels == NULL) {
ERR_ALLOC_FAILED;
knot_dname_free(&dname);
return NULL;
}
memcpy(dname->name, name, i + 1);
dname->size = i + 1;
/*! \todo Why l + 1 ?? */
dname->labels = (uint8_t *)malloc((l + 1) * sizeof(uint8_t));
if (dname->labels == NULL) {
ERR_ALLOC_FAILED;
knot_dname_free(&dname);
return NULL;
}
memcpy(dname->labels, labels, l + 1);
dname->size = i + 1;
dname->label_count = l;
dname->node = node;
return dname;
......
......@@ -291,24 +291,25 @@ static int knot_packet_parse_question(const uint8_t *wire, size_t *pos,
dbg_packet_verb("Parsing dname starting on position %zu and "
"%zu bytes long.\n", *pos, i - *pos + 1);
dbg_packet_verb("Alloc: %d\n", alloc);
size_t bp = *pos;
if (alloc) {
question->qname = knot_dname_parse_from_wire(wire, pos,
i - *pos + 1,
i + 1,
NULL, NULL);
if (question->qname == NULL) {
return KNOT_ENOMEM;
}
} else {
int res = knot_dname_parse_from_wire(wire, pos,
i - *pos + 1,
void *parsed = knot_dname_parse_from_wire(wire, pos,
i + 1,
NULL, question->qname);
if (res != KNOT_EOK) {
assert(res != KNOT_EINVAL);
return res;
if (!parsed) {
return KNOT_EMALF;
}
}
//*pos = i + 1;
if (*pos != i + 1) {
dbg_packet("Parsed dname expected len=%zu, parsed=%zu.\n", i+1-bp, *pos-bp);
}
question->qtype = knot_wire_read_u16(wire + i + 1);
question->qclass = knot_wire_read_u16(wire + i + 3);
*pos += 4;
......
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