From 9b69f3852788f0ae028b819e2ffa5af2ff7bcc95 Mon Sep 17 00:00:00 2001 From: Bjorn Svensson Date: Wed, 20 Jul 2022 13:23:45 +0200 Subject: [PATCH] Fix leak in r3_tree_insert_pathl_ex() (#150) Includes a testcase triggering the issue when building with the leak sanitizer. Resource leak (CID 355063) found by Coverity, --- src/node.c | 8 ++++++-- tests/check_tree.c | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/node.c b/src/node.c index 46a70b2..c5a3bfd 100644 --- a/src/node.c +++ b/src/node.c @@ -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; } diff --git a/tests/check_tree.c b/tests/check_tree.c index 9533757..b2ddf8c 100644 --- a/tests/check_tree.c +++ b/tests/check_tree.c @@ -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) { @@ -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);