diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2016-07-04 23:59:06 +0200 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2016-07-05 02:51:46 +0200 |
commit | f416c85366a3e85e7f7576398f55595de51adc52 (patch) | |
tree | 8fdb0a91865eaa8a58c84d3e97112601b318b460 | |
parent | 4c228763c31255ea9bd3cd1bbd93f5cf7ab9f54b (diff) | |
download | blogc-f416c85366a3e85e7f7576398f55595de51adc52.tar.gz blogc-f416c85366a3e85e7f7576398f55595de51adc52.tar.bz2 blogc-f416c85366a3e85e7f7576398f55595de51adc52.zip |
renderer: utils: fixed memory bugs found by clang static analyzer
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | src/renderer.c | 8 | ||||
-rw-r--r-- | src/utils.c | 10 | ||||
-rw-r--r-- | tests/check_utils.c | 1 |
4 files changed, 13 insertions, 9 deletions
@@ -71,4 +71,7 @@ blogc-*.rpm # git-version-gen /.version +# scan-build reports +*.plist + /build/ diff --git a/src/renderer.c b/src/renderer.c index 351dfb1..c9cc15b 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -111,10 +111,10 @@ blogc_format_variable(const char *name, sb_trie_t *global, sb_trie_t *local, else value = blogc_get_variable(var, global, local); - free(var); - - if (value == NULL) + if (value == NULL) { + free(var); return NULL; + } char *rv = NULL; @@ -132,6 +132,8 @@ blogc_format_variable(const char *name, sb_trie_t *global, sb_trie_t *local, rv = sb_strdup(value); } + free(var); + if (len > 0) { char *tmp = sb_strndup(rv, len); free(rv); diff --git a/src/utils.c b/src/utils.c index d7362a6..9a39c61 100644 --- a/src/utils.c +++ b/src/utils.c @@ -622,22 +622,19 @@ sb_trie_foreach_node(sb_trie_node_t *node, sb_string_t *str, return; if (node->key == '\0') { - char *tmp = sb_string_free(str, false); - func(tmp, node->data, user_data); - free(tmp); + func(str->str, node->data, user_data); + return; } if (node->child != NULL) { sb_string_t *child = sb_string_dup(str); child = sb_string_append_c(child, node->key); sb_trie_foreach_node(node->child, child, func, user_data); + sb_string_free(child, true); } if (node->next != NULL) sb_trie_foreach_node(node->next, str, func, user_data); - - if (node->child != NULL && node->next == NULL) - sb_string_free(str, true); } @@ -650,4 +647,5 @@ sb_trie_foreach(sb_trie_t *trie, sb_trie_foreach_func_t func, sb_string_t *str = sb_string_new(); sb_trie_foreach_node(trie->root, str, func, user_data); + sb_string_free(str, true); } diff --git a/tests/check_utils.c b/tests/check_utils.c index 31087f1..b76d171 100644 --- a/tests/check_utils.c +++ b/tests/check_utils.c @@ -938,6 +938,7 @@ test_trie_foreach(void **state) sb_trie_foreach(NULL, mock_foreach, "foo"); sb_trie_foreach(trie, NULL, "foo"); sb_trie_foreach(NULL, NULL, "foo"); + assert_int_equal(counter, 7); sb_trie_free(trie); } |