]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
format-table: fix potentail memleak and invalid-free
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 6 Mar 2021 01:10:31 +0000 (10:10 +0900)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 6 Mar 2021 08:23:38 +0000 (09:23 +0100)
src/shared/format-table.c

index fdb5887a1aaea8ba716fb969cfb5b231a418b921..ae56cd3d6788fea799e97ef95cf1b42751aab922 100644 (file)
@@ -1317,9 +1317,9 @@ static int table_data_compare(const size_t *a, const size_t *b, Table *t) {
 }
 
 static char* format_strv_width(char **strv, size_t column_width) {
+        _cleanup_free_ char *buf = NULL; /* buf must be freed after f */
         _cleanup_fclose_ FILE *f = NULL;
         size_t sz = 0;
-        _cleanup_free_ char *buf = NULL;
 
         f = open_memstream_unlocked(&buf, &sz);
         if (!f)
@@ -2320,8 +2320,8 @@ int table_print(Table *t, FILE *f) {
 }
 
 int table_format(Table *t, char **ret) {
+        _cleanup_free_ char *buf = NULL;
         _cleanup_fclose_ FILE *f = NULL;
-        char *buf = NULL;
         size_t sz = 0;
         int r;
 
@@ -2335,7 +2335,7 @@ int table_format(Table *t, char **ret) {
 
         f = safe_fclose(f);
 
-        *ret = buf;
+        *ret = TAKE_PTR(buf);
 
         return 0;
 }