Skip to content

Commit

Permalink
Fix leak in r3_tree_insert_pathl_ex() (#150)
Browse files Browse the repository at this point in the history
Includes a testcase triggering the issue when building with
the leak sanitizer.

Resource leak (CID 355063) found by Coverity,
  • Loading branch information
bjosv committed Jul 20, 2022
1 parent 25cc816 commit 9b69f38
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,12 @@ R3Node * r3_tree_insert_pathl_ex(R3Node *tree, const char *path, unsigned int pa
char *err = NULL;
e = r3_node_find_common_prefix(tree, path, path_len, &prefix_len, &err);
if (err) {
// copy the error message pointer
if (errstr) *errstr = err;
if (errstr) {
// copy the error message pointer
*errstr = err;
} else {
free(err);
}
return NULL;
}

Expand Down
18 changes: 18 additions & 0 deletions tests/check_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -595,8 +595,25 @@ START_TEST (test_insert_pathl_fail)
}
END_TEST

START_TEST (test_insert_pathl_fail2)
{
R3Node * n = r3_tree_create(10);
char *errstr = NULL;
R3Node * ret;

ret = r3_tree_insert_pathl_ex(n, "/foo", strlen("/foo"), 0, 0, 0, NULL);
ck_assert(ret);

/* Insert an incomplete pattern without requesting an error string */
ret = r3_tree_insert_pathl_ex(n, "/foo/{name:\\d{5}", strlen("/foo/{name:\\d{5}"), 0, 0, 0, NULL);
ck_assert(ret == NULL);

r3_tree_compile(n, &errstr);
ck_assert(errstr == NULL);

r3_tree_free(n);
}
END_TEST

START_TEST (test_insert_pathl)
{
Expand Down Expand Up @@ -862,6 +879,7 @@ Suite* r3_suite (void) {
tcase = tcase_create("insert_testcase");
tcase_add_test(tcase, test_insert_pathl);
tcase_add_test(tcase, test_insert_pathl_fail);
tcase_add_test(tcase, test_insert_pathl_fail2);
tcase_add_test(tcase, test_node_construct_and_free);
suite_add_tcase(suite, tcase);

Expand Down

0 comments on commit 9b69f38

Please sign in to comment.