From: Ian Campbell Date: Tue, 3 Aug 2010 17:10:28 +0000 (+0100) Subject: xl: fix memory leaks in xl create X-Git-Tag: archive/raspbian/4.8.0-1+rpi1~1^2~11693 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=4fb44903e86bf86823e46cc37482ff1387af3d59;p=xen.git xl: fix memory leaks in xl create Found using "valgrind xl create -n ..." and "valgrind xl create -e ..." freeing config_data solves: ==18276== 944 bytes in 1 blocks are definitely lost in loss record 12 of 12 ==18276== at 0x4022F0A: malloc (vg_replace_malloc.c:236) ==18276== by 0x404AEC1: libxl_read_file_contents (libxl_utils.c:258) ==18276== by 0x8056865: create_domain (xl_cmdimpl.c:1314) ==18276== by 0x8057E2D: main_create (xl_cmdimpl.c:3135) ==18276== by 0x804B2FB: main (xl.c:76) ==18276== Adding free_domain_config() solves the following (plus presumably others which didn't trigger because I have no devices of that type). d_config->disks: ==18276== 61 (32 direct, 29 indirect) bytes in 1 blocks are definitely lost in loss record 9 of 12 ==18276== at 0x4022F0A: malloc (vg_replace_malloc.c:236) ==18276== by 0x4022F94: realloc (vg_replace_malloc.c:525) ==18276== by 0x804E2D3: parse_config_data (xl_cmdimpl.c:715) ==18276== by 0x8056A7C: create_domain (xl_cmdimpl.c:1347) ==18276== by 0x8057E2D: main_create (xl_cmdimpl.c:3135) ==18276== by 0x804B2FB: main (xl.c:76) d_config->vifs: ==18276== 76 (48 direct, 28 indirect) bytes in 1 blocks are definitely lost in loss record 10 of 12 ==18276== at 0x4022F0A: malloc (vg_replace_malloc.c:236) ==18276== by 0x4022F94: realloc (vg_replace_malloc.c:525) ==18276== by 0x804E665: parse_config_data (xl_cmdimpl.c:779) ==18276== by 0x8056A7C: create_domain (xl_cmdimpl.c:1347) ==18276== by 0x8057E2D: main_create (xl_cmdimpl.c:3135) ==18276== by 0x804B2FB: main (xl.c:76) Signed-off-by: Ian Campbell Signed-off-by: Stefano Stabellini --- diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c index 1680c2eba6..a2aca08566 100644 --- a/tools/libxl/xl_cmdimpl.c +++ b/tools/libxl/xl_cmdimpl.c @@ -143,6 +143,16 @@ struct domain_config { enum action_on_shutdown on_crash; }; +static void free_domain_config(struct domain_config *d_config) +{ + free(d_config->disks); + free(d_config->vifs); + free(d_config->vif2s); + free(d_config->pcidevs); + free(d_config->vfbs); + free(d_config->vkbs); +} + /* Optional data, in order: * 4 bytes uint32_t config file size * n bytes config file in Unix text file format @@ -1346,8 +1356,9 @@ static int create_domain(struct domain_create *dom_info) parse_config_data(config_file, config_data, config_len, &d_config, &dm_info); + ret = 0; if (dom_info->dryrun) - return 0; + goto out; if (migrate_fd >= 0) { if (d_config.c_info.name) { @@ -1477,8 +1488,9 @@ start: if (!paused) libxl_domain_unpause(&ctx, domid); + ret = domid; /* caller gets success in parent */ if (!daemonize) - return domid; /* caller gets success in parent */ + goto out; if (need_daemon) { char *fullname, *name; @@ -1605,6 +1617,12 @@ start: error_out: if (domid) libxl_domain_destroy(&ctx, domid, 0); +out: + + free_domain_config(&d_config); + + free(config_data); + return ret; } @@ -2143,6 +2161,7 @@ void list_domains_details(const libxl_dominfo *info, int nb_domain) memset(&d_config, 0x00, sizeof(d_config)); parse_config_data(config_file, (char *)data, len, &d_config, &dm_info); printf_info(info[i].domid, &d_config, &dm_info); + free_domain_config(&d_config); free(data); free(config_file); }