From fd556f4e64c9e13d9d879abba40d0efb084fa395 Mon Sep 17 00:00:00 2001 From: Stone Tickle Date: Sat, 3 Jun 2023 10:00:44 -0400 Subject: [PATCH] remove memory leaks from editorconfig parser --- src/formats/editorconfig.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/formats/editorconfig.c b/src/formats/editorconfig.c index 39529007..3f3bb1dc 100644 --- a/src/formats/editorconfig.c +++ b/src/formats/editorconfig.c @@ -15,6 +15,7 @@ #include "formats/editorconfig.h" #include "formats/ini.h" #include "lang/string.h" +#include "platform/mem.h" #include "platform/path.h" struct parse_editorconfig_ctx { @@ -290,6 +291,9 @@ try_parse_editorconfig(struct source *src, struct editorconfig_opts *opts) const char *indent_style = NULL, *indent_size = NULL, *tab_width = NULL; struct source cfg_src = { 0 }; + struct darr garbage; + darr_init(&garbage, 16, sizeof(void *)); + while (true) { path_join(0, &path, wd.buf, ".editorconfig"); if (fs_file_exists(path.buf)) { @@ -298,13 +302,12 @@ try_parse_editorconfig(struct source *src, struct editorconfig_opts *opts) }; char *cfg_buf = NULL; - if (!fs_read_entire_file(path.buf, &cfg_src)) { - goto ret; - } else if (!ini_parse(path.buf, &cfg_src, &cfg_buf, editorconfig_cfg_parse_cb, &editorconfig_ctx)) { + if (!ini_parse(path.buf, &cfg_src, &cfg_buf, editorconfig_cfg_parse_cb, &editorconfig_ctx)) { goto ret; - } + darr_push(&garbage, &cfg_buf); + fs_source_destroy(&cfg_src); cfg_src = (struct source){ 0 }; @@ -370,6 +373,11 @@ try_parse_editorconfig(struct source *src, struct editorconfig_opts *opts) opts->indent_by = buf; ret: + for (i = 0; i < garbage.len; ++i) { + z_free(*(void **)darr_get(&garbage, i)); + } + darr_destroy(&garbage); + fs_source_destroy(&cfg_src); sbuf_destroy(&wd); sbuf_destroy(&path); -- 2.45.2