From a6a0b9ba164f68dd79782f054604102017bc9c15 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Fri, 23 Nov 2012 12:12:33 +0000 Subject: lib: Fix memory leak and simplify command code. Fix the following memory leak found by valgrind: ==13629== 498 (112 direct, 386 indirect) bytes in 1 blocks are definitely lost in loss record 99 of 110 ==13629== at 0x4A06B2F: calloc (vg_replace_malloc.c:593) ==13629== by 0x4CA564E: guestfs_safe_calloc (alloc.c:71) ==13629== by 0x4CA9B02: guestfs___new_command (command.c:143) ==13629== by 0x4CA66E9: guestfs___build_appliance (appliance.c:690) ==13629== by 0x4CBD1B9: launch_libvirt (launch-libvirt.c:188) ==13629== by 0x402E7E: main (virt-filesystems.c:349) Also adjust the command code in several places to make it simpler. We can almost always call guestfs___cmd_close right after guestfs___cmd_run, avoiding any need to close the handle along error paths. Tested by running the test suite under valgrind. --- src/appliance.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'src/appliance.c') diff --git a/src/appliance.c b/src/appliance.c index 8a22064b..e7ba60df 100644 --- a/src/appliance.c +++ b/src/appliance.c @@ -286,12 +286,10 @@ calculate_supermin_checksum (guestfs_h *g, const char *supermin_path) guestfs___cmd_set_stdout_callback (cmd, read_checksum, checksum, 0); r = guestfs___cmd_run (cmd); + guestfs___cmd_close (cmd); /* Errors here are non-fatal, so we don't need to call error(). */ - if (r == -1) { - guestfs___cmd_close (cmd); + if (r == -1) return NULL; - } - guestfs___cmd_close (cmd); debug (g, "checksum of existing appliance: %s", checksum); @@ -707,6 +705,7 @@ run_supermin_helper (guestfs_h *g, const char *supermin_path, guestfs___cmd_add_arg_format (cmd, "%s/root", cachedir); r = guestfs___cmd_run (cmd); + guestfs___cmd_close (cmd); if (r == -1) return -1; if (!WIFEXITED (r) || WEXITSTATUS (r) != 0) { -- cgit