summaryrefslogtreecommitdiffstats
path: root/src/inspect-icon.c
diff options
context:
space:
mode:
authorRichard W.M. Jones <rjones@redhat.com>2012-11-23 12:12:33 +0000
committerRichard W.M. Jones <rjones@redhat.com>2012-11-23 12:12:33 +0000
commita6a0b9ba164f68dd79782f054604102017bc9c15 (patch)
treefd19a529e2bbd66c81ee578bde14a323d0b874f3 /src/inspect-icon.c
parentfae8d7cafb66e0da697cde71f7e9444165c76ff7 (diff)
downloadlibguestfs-a6a0b9ba164f68dd79782f054604102017bc9c15.tar.gz
libguestfs-a6a0b9ba164f68dd79782f054604102017bc9c15.tar.xz
libguestfs-a6a0b9ba164f68dd79782f054604102017bc9c15.zip
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.
Diffstat (limited to 'src/inspect-icon.c')
-rw-r--r--src/inspect-icon.c25
1 files changed, 6 insertions, 19 deletions
diff --git a/src/inspect-icon.c b/src/inspect-icon.c
index 4831ab1b..4b7f43a2 100644
--- a/src/inspect-icon.c
+++ b/src/inspect-icon.c
@@ -364,7 +364,7 @@ icon_cirros (guestfs_h *g, struct inspect_fs *fs, size_t *size_r)
char *type = NULL;
char *local = NULL;
char *pngfile = NULL;
- struct command *cmd = NULL;
+ struct command *cmd;
int r;
r = guestfs_exists (g, CIRROS_LOGO);
@@ -392,6 +392,7 @@ icon_cirros (guestfs_h *g, struct inspect_fs *fs, size_t *size_r)
guestfs___cmd_add_string_unquoted (cmd, " | " PNMTOPNG " > ");
guestfs___cmd_add_string_quoted (cmd, pngfile);
r = guestfs___cmd_run (cmd);
+ guestfs___cmd_close (cmd);
if (r == -1)
goto out;
if (!WIFEXITED (r) || WEXITSTATUS (r) != 0)
@@ -407,8 +408,6 @@ icon_cirros (guestfs_h *g, struct inspect_fs *fs, size_t *size_r)
free (pngfile);
free (local);
free (type);
- if (cmd)
- guestfs___cmd_close (cmd);
return ret;
}
@@ -438,7 +437,7 @@ icon_windows_xp (guestfs_h *g, struct inspect_fs *fs, const char *explorer,
{
char *ret;
char *pngfile = NULL;
- struct command *cmd = NULL;
+ struct command *cmd;
int r;
pngfile = safe_asprintf (g, "%s/windows-xp-icon.png", g->tmpdir);
@@ -450,14 +449,12 @@ icon_windows_xp (guestfs_h *g, struct inspect_fs *fs, const char *explorer,
" | " BMPTOPNM " | " PNMTOPNG " > ");
guestfs___cmd_add_string_quoted (cmd, pngfile);
r = guestfs___cmd_run (cmd);
+ guestfs___cmd_close (cmd);
if (r == -1)
goto error;
if (!WIFEXITED (r) || WEXITSTATUS (r) != 0)
goto not_found;
- guestfs___cmd_close (cmd);
- cmd = NULL;
-
if (read_whole_file (g, pngfile, &ret, size_r) == -1)
goto error;
@@ -466,14 +463,10 @@ icon_windows_xp (guestfs_h *g, struct inspect_fs *fs, const char *explorer,
return ret;
error:
- if (cmd)
- guestfs___cmd_close (cmd);
free (pngfile);
return NULL;
not_found:
- if (cmd)
- guestfs___cmd_close (cmd);
free (pngfile);
return NOT_FOUND;
}
@@ -484,7 +477,7 @@ icon_windows_7 (guestfs_h *g, struct inspect_fs *fs, const char *explorer,
{
char *ret;
char *pngfile = NULL;
- struct command *cmd = NULL;
+ struct command *cmd;
int r;
pngfile = safe_asprintf (g, "%s/windows-7-icon.png", g->tmpdir);
@@ -498,14 +491,12 @@ icon_windows_7 (guestfs_h *g, struct inspect_fs *fs, const char *explorer,
PNMTOPNG " > ");
guestfs___cmd_add_string_quoted (cmd, pngfile);
r = guestfs___cmd_run (cmd);
+ guestfs___cmd_close (cmd);
if (r == -1)
goto error;
if (!WIFEXITED (r) || WEXITSTATUS (r) != 0)
goto not_found;
- guestfs___cmd_close (cmd);
- cmd = NULL;
-
if (read_whole_file (g, pngfile, &ret, size_r) == -1)
goto error;
@@ -514,14 +505,10 @@ icon_windows_7 (guestfs_h *g, struct inspect_fs *fs, const char *explorer,
return ret;
error:
- if (cmd)
- guestfs___cmd_close (cmd);
free (pngfile);
return NULL;
not_found:
- if (cmd)
- guestfs___cmd_close (cmd);
free (pngfile);
return NOT_FOUND;
}