From: Karel Zak Date: Tue, 24 Oct 2023 18:13:29 +0000 (+0200) Subject: tests: fix memory leak in scols fromfile X-Git-Tag: v2.40-rc1~179 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=765a39dad539e5c2891efede997dbe977256c9bd;p=thirdparty%2Futil-linux.git tests: fix memory leak in scols fromfile Signed-off-by: Karel Zak --- diff --git a/libsmartcols/samples/fromfile.c b/libsmartcols/samples/fromfile.c index 631262f841..d47ca477c0 100644 --- a/libsmartcols/samples/fromfile.c +++ b/libsmartcols/samples/fromfile.c @@ -148,21 +148,22 @@ static int parse_column_data(FILE *f, struct libscols_table *tb, int col) break; p = strrchr(str, '\n'); - if (p) { + if (p) *p = '\0'; - len = p - str; - } if (!*str) continue; /* convert \x?? to real bytes */ if (strstr(str, "\\x")) { struct libscols_cell *ce = scols_line_get_cell(ln, col); - char *buf = xcalloc(1, ++len); - - len = unhexmangle_to_buffer(str, buf, len); - if (len) - rc = scols_cell_refer_memory(ce, buf, len); + size_t sz = len + 1; + char *buf = xcalloc(1, sz); + + sz = unhexmangle_to_buffer(str, buf, sz); + if (sz) + rc = scols_cell_refer_memory(ce, buf, sz); + else + free(buf); } else rc = scols_line_set_data(ln, col, str); if (rc)