From 71bf4b291dfed04a710b561326107d33f4d7e4e3 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 12 Dec 2023 20:38:34 +0100 Subject: [PATCH] libsmartcols: free after error in filter sample Signed-off-by: Karel Zak --- libsmartcols/samples/fromfile.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/libsmartcols/samples/fromfile.c b/libsmartcols/samples/fromfile.c index f9c9fdff91..f627df872d 100644 --- a/libsmartcols/samples/fromfile.c +++ b/libsmartcols/samples/fromfile.c @@ -131,9 +131,11 @@ static struct libscols_filter *init_filter( if (!f) err(EXIT_FAILURE, "failed to allocate filter"); - if (scols_filter_parse_string(f, query) != 0) - errx(EXIT_FAILURE, "failed to parse filter: %s", - scols_filter_get_errmsg(f)); + if (scols_filter_parse_string(f, query) != 0) { + warnx("failed to parse filter: %s", scols_filter_get_errmsg(f)); + scols_unref_filter(f); + return NULL; + } itr = scols_new_iter(SCOLS_ITER_FORWARD); if (!itr) @@ -223,7 +225,7 @@ static void __attribute__((__noreturn__)) usage(void) int main(int argc, char *argv[]) { struct libscols_table *tb; - int c, n, nlines = 0; + int c, n, nlines = 0, rc; int parent_col = -1, id_col = -1; int fltr_dump = 0; const char *fltr_str = NULL; @@ -332,8 +334,13 @@ int main(int argc, char *argv[]) scols_unref_line(ln); } - if (fltr_str) + if (fltr_str) { fltr = init_filter(tb, fltr_str, fltr_dump); + if (!fltr) { + rc = EXIT_FAILURE; + goto done; + } + } n = 0; @@ -357,8 +364,9 @@ int main(int argc, char *argv[]) apply_filter(tb, fltr); scols_print_table(tb); - + rc = EXIT_SUCCESS; +done: scols_unref_filter(fltr); scols_unref_table(tb); - return EXIT_SUCCESS; + return rc; } -- 2.47.3