From 10d2225ca32286aaf5089e606b50cd4ff3e1e586 Mon Sep 17 00:00:00 2001 From: Greg Beard Date: Thu, 14 Mar 2019 22:06:33 +0000 Subject: [PATCH] This PR fixes a number of memory issues reported by *AddressSanitizer*. *ASan* detects lots of leaks relating to the *context stack*. Fixed by adding `free(context)` to `POP_CONTEXT`, and clearing any remaining items on the stack after parsing has completed. The `examples/example.toml` file fails *ASan*, asserting on invalid memory access. Fixed by ensuring that a list node's name is set to `NULL` if no name was parsed. --- toml_parse.rl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/toml_parse.rl b/toml_parse.rl index 2d335e9..ee4348d 100644 --- a/toml_parse.rl +++ b/toml_parse.rl @@ -24,6 +24,7 @@ struct toml_stack_item { struct toml_stack_item* context = CONTEXT(&context_stack); \ list_del(&context->list); \ x = context->node; \ + free(context); \ } while (0) static size_t @@ -301,6 +302,7 @@ add_node_to_tree(struct list_head* context_stack, struct toml_node* node, char* context->list_type = TOML_LIST; item->node.type = TOML_LIST; + item->node.name = NULL; if (name) { item->node.name = name; @@ -769,6 +771,10 @@ toml_parse(struct toml_node* toml_root, char* buf, int buflen) %% write exec; + while ((root = CONTEXT(&context_stack))) { + POP_CONTEXT(toml_root); + } + if (malloc_error) { fprintf(stderr, "malloc failed, line %d\n", cur_line); return 1;