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

Fixed unchecked realloc in trie, harmless minor problems.

parent 0d0c99cb
No related branches found
No related tags found
No related merge requests found
......@@ -104,7 +104,14 @@ static node_ptr hattrie_consume_ns(node_ptr **s, size_t *sp, size_t slen,
bs = malloc(slen * sizeof(node_ptr));
memcpy(bs, *s, (slen/2) * sizeof(node_ptr));
} else { /* points to heap memory already */
bs = realloc(bs, slen * sizeof(node_ptr));
node_ptr *bs_new = realloc(bs, slen * sizeof(node_ptr));
/* \note tricky, hattrie should be slowly moved from 'never-expect-malloc-fail' state */
if (bs_new == NULL) {
*sp = 0; /* caller will get take care of freeing old 'bs' */
return bs[0]; /* return root node, so the search fails */
} else {
bs = bs_new;
}
}
/* update parent pointer on resize */
*s = bs;
......@@ -154,14 +161,13 @@ static inline int hattrie_clrval(hattrie_t *T, node_ptr n)
static value_t* hattrie_find_rightmost(node_ptr node)
{
/* iterate children from right */
value_t *ret = NULL;
if (*node.flag & NODE_TYPE_TRIE) {
for (int i = TRIE_MAXCHAR; i > -1; --i) {
/* skip repeated pointers to hybrid bucket */
if (i < TRIE_MAXCHAR && node.t->xs[i].t == node.t->xs[i + 1].t)
continue;
/* nest if trie */
ret = hattrie_find_rightmost(node.t->xs[i]);
value_t *ret = hattrie_find_rightmost(node.t->xs[i]);
if (ret) {
return ret;
}
......@@ -317,7 +323,7 @@ hattrie_t* hattrie_dup(const hattrie_t* T, value_t (*nval)(value_t))
/*! \todo could be probably implemented faster */
size_t l = 0;
const char *k = 0;
const char *k = NULL;
hattrie_iter_t *i = hattrie_iter_begin(T, false);
while (!hattrie_iter_finished(i)) {
k = hattrie_iter_key(i, &l);
......
......@@ -30,7 +30,7 @@ static int universal_cmp(uint32_t k1, uint32_t k2, hhash_t *tbl);
* Key is variable-sized string. */
#define KEY_VAL(p) (p)
#define KEY_LEN(p) ((p) + HHVAL_LEN)
#define KEY_STR(p) ((p) + HHKEY_LEN)
#define KEY_STR(p) ((char*)(p) + HHKEY_LEN)
/*! \brief Helper function to read key length. */
static inline uint16_t key_readlen(const void *k)
......
......@@ -118,12 +118,11 @@ int sockaddr_tostr(const sockaddr_t *addr, char *dst, size_t size)
return -1;
}
/* Minimum length. */
size_t minlen = INET_ADDRSTRLEN;
/* Check unsupported IPv6. */
#ifndef DISABLE_IPV6
minlen = INET6_ADDRSTRLEN;
size_t minlen = INET6_ADDRSTRLEN;
#else
size_t minlen = INET_ADDRSTRLEN;
#endif
/* Check minimum length. */
......
......@@ -374,7 +374,7 @@ static int tsig_parse_file(knot_tsig_key_t *k, const char *f)
ret = KNOT_EMALF;
break;
}
line[llen++] = '\0';
line[llen] = '\0';
ret = tsig_parse_line(k, line);
llen = 0;
} else {
......
......@@ -262,12 +262,14 @@ static int server_bind_sockets(server_t *s)
/* Find already matching interface. */
int found_match = 0;
conf_iface_t *cfg_if = (conf_iface_t*)n;
if (s->ifaces) WALK_LIST(m, s->ifaces->u) {
/* Matching port and address. */
if (cfg_if->port == m->port) {
if (strcmp(cfg_if->address, m->addr) == 0) {
found_match = 1;
break;
if (s->ifaces) {
WALK_LIST(m, s->ifaces->u) {
/* Matching port and address. */
if (cfg_if->port == m->port) {
if (strcmp(cfg_if->address, m->addr) == 0) {
found_match = 1;
break;
}
}
}
}
......
......@@ -425,7 +425,7 @@ bool knot_dname_is_sub(const knot_dname_t *sub, const knot_dname_t *domain)
if (sub_l < 0 || domain_l < 0)
return false;
assert(sub >= 0 && sub_l <= KNOT_DNAME_MAXLABELS);
assert(sub_l >= 0 && sub_l <= KNOT_DNAME_MAXLABELS);
assert(domain_l >= 0 && domain_l <= KNOT_DNAME_MAXLABELS);
/* Subdomain must have more labels as parent. */
......
......@@ -120,7 +120,6 @@ int main(int argc, char *argv[])
/* 18-19. dname cat (valid) */
w = "\x03""cat";
len = 5;
d = knot_dname_copy((const uint8_t *)w);
t = "*";
d2 = knot_dname_from_str(t);
......
......@@ -84,7 +84,7 @@ void* thr_action(void *arg)
int main(int argc, char *argv[])
{
plan(11);
plan(12);
/* 1. Create fdset. */
fdset_t set;
......@@ -96,6 +96,7 @@ int main(int argc, char *argv[])
ret = pipe(fds);
ok(ret >= 0, "fdset: pipe() works");
ret = pipe(tmpfds);
ok(ret >= 0, "fdset: 2nd pipe() works");
/* 3. Add fd to set. */
ret = fdset_add(&set, fds[0], POLLIN, NULL);
......
......@@ -77,14 +77,13 @@ int main(int argc, char *argv[])
}
/* Test 1: Create */
unsigned passed = 1;
value_t *v = NULL;
hattrie_t *t = hattrie_create();
ok(t != NULL, "hattrie: create");
/* Test 2: Insert */
unsigned passed = 1;
unsigned really_inserted = 0;
passed = 1;
for (unsigned i = 0; i < count; ++i) {
v = hattrie_get(t, items[i], strlen(items[i]));
if (!v) {
......@@ -166,13 +165,12 @@ int main(int argc, char *argv[])
/* Sorted iteration. */
size_t len = 0, prev_len = 0;
const char *cur = NULL;
char *prev = NULL;
counted = 0;
hattrie_build_index(t);
it = hattrie_iter_begin(t, true);
while (!hattrie_iter_finished(it)) {
cur = hattrie_iter_key(it, &len);
const char *cur = hattrie_iter_key(it, &len);
if (!str_check_sort(prev, cur, prev_len, len)) {
diag("(%zu)'%s' < (%zu)'%s' FAIL\n",
prev_len, prev, len, cur);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment