tools: check return value of asprintf() in xenhypfs
authorJuergen Gross <jgross@suse.com>
Tue, 2 Jun 2020 06:00:20 +0000 (08:00 +0200)
committerWei Liu <wl@xen.org>
Tue, 2 Jun 2020 12:05:13 +0000 (12:05 +0000)
asprintf() can fail, so check its return value. Additionally fix a
memory leak in xenhypfs.

Reported-by: Andrew Cooper <andrew.cooper3@citrix.com>
Signed-off-by: Juergen Gross <jgross@suse.com>
Acked-by: Wei Liu <wl@xen.org>
Release-acked-by: Paul Durrant <paul@xen.org>
tools/misc/xenhypfs.c

index 158b901f42c97a7ae33b817ddbcce44ee8a868fa..5145b8969fe358174ca5c06fa190d7ef2888fd45 100644 (file)
@@ -148,9 +148,14 @@ static int xenhypfs_tree_sub(char *path, unsigned int depth)
         printf("%*s%s%s\n", depth * 2, "", ent[i].name,
                ent[i].type == xenhypfs_type_dir ? "/" : "");
         if (ent[i].type == xenhypfs_type_dir) {
-            asprintf(&p, "%s%s%s", path, (depth == 1) ? "" : "/", ent[i].name);
+            if (asprintf(&p, "%s%s%s", path, (depth == 1) ? "" : "/",
+                         ent[i].name) < 0) {
+                ret = 2;
+                break;
+            }
             if (xenhypfs_tree_sub(p, depth + 1))
                 ret = 2;
+            free(p);
         }
     }