]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
tests: fix memory leak in scols fromfile
authorKarel Zak <kzak@redhat.com>
Tue, 24 Oct 2023 18:13:29 +0000 (20:13 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 24 Oct 2023 18:13:29 +0000 (20:13 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/samples/fromfile.c

index 631262f841a96c4bda6296ce6788f5e951208ba0..d47ca477c030ac25f9e46e661eb11f7aa175c04e 100644 (file)
@@ -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)