Skip to contentSkip to conversation

xl: fix two memory leaks

PATCH Superseded Wei Liu 2 participants 4 messages Nov 30, 2014 – Dec 1, 2014

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

PATCH 1/1 xl: fix two memory leaks

There are two invocations of libxl_basename, which returns a malloc'ed string. Those strings should be freed after used.

Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
---
 tools/libxl/xl_cmdimpl.c |    9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tools/libxl/xl_cmdimpl.c +7 -2
index 9afef3f..716a865 100644
@@ -920,6 +920,7 @@ static void parse_config_data(const char *config_source,
     int pci_permissive = 0;
     int pci_seize = 0;
     int i, e;
+    const char *basename;
 
     libxl_domain_create_info *c_info = &d_config->c_info;
     libxl_domain_build_info *b_info = &d_config->b_info;
@@ -1116,13 +1117,16 @@ static void parse_config_data(const char *config_source,
 
     switch(b_info->type) {
     case LIBXL_DOMAIN_TYPE_HVM:
-        if (!strcmp(libxl_basename(b_info->kernel), "hvmloader")) {
+        basename = libxl_basename(b_info->kernel);
+        if (!strcmp(basename, "hvmloader")) {
             fprintf(stderr, "WARNING: you seem to be using \"kernel\" "
                     "directive to override HVM guest firmware. Ignore "
                     "that. Use \"firmware_override\" instead if you "
                     "really want a non-default firmware\n");
             b_info->kernel = NULL;
         }
+        free((void*)basename);
+        basename = NULL;
 
         xlu_cfg_replace_string (config, "firmware_override",
                                 &b_info->u.hvm.firmware, 0);
@@ -7021,7 +7025,7 @@ int main_cpupoolcreate(int argc, char **argv)
     int config_len = 0;
     XLU_Config *config;
     const char *buf;
-    const char *name;
+    const char *name = NULL;
     uint32_t poolid;
     libxl_scheduler sched = 0;
     XLU_ConfigList *cpus;
@@ -7196,6 +7200,7 @@ out_cfg:
     xlu_cfg_destroy(config);
 out:
     free(config_data);
+    free((void*)name);
     return rc;
 }
 
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 26 lines · Wei Liu

There are two invocations of libxl_basename, which returns a malloc'ed string. Those strings should be freed after used.

Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
---
 tools/libxl/xl_cmdimpl.c |    9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tools/libxl/xl_cmdimpl.c +1 -0
index 9afef3f..716a865 100644
@@ -920,6 +920,7 @@ static void parse_config_data(const char *config_source,
     int pci_permissive = 0;
     int pci_seize = 0;
     int i, e;
+    const char *basename;
 
     libxl_domain_create_info *c_info = &d_config->c_info;
     libxl_domain_build_info *b_info = &d_config->b_info;
@@ -1116,13 +1117,16 @@ static void parse_config_data(const char *config_source,
 
     switch(b_info->type) {
     case LIBXL_DOMAIN_TYPE_HVM:

tools/libxl/xl_cmdimpl.c static void parse_config_data(const char *config_source, · line 1128

Quoted 10 lines
tools/libxl/xl_cmdimpl.c +3 -1
-        if (!strcmp(libxl_basename(b_info->kernel), "hvmloader")) {
+        basename = libxl_basename(b_info->kernel);
+        if (!strcmp(basename, "hvmloader")) {
             fprintf(stderr, "WARNING: you seem to be using \"kernel\" "
                     "directive to override HVM guest firmware. Ignore "
                     "that. Use \"firmware_override\" instead if you "
                     "really want a non-default firmware\n");
             b_info->kernel = NULL;
         }
+        free((void*)basename);

I think you should un-const the declaration (in both cases) rather than adding casts.

Quoted 21 lines
tools/libxl/xl_cmdimpl.c +3 -1
+        basename = NULL;
 
         xlu_cfg_replace_string (config, "firmware_override",
                                 &b_info->u.hvm.firmware, 0);
@@ -7021,7 +7025,7 @@ int main_cpupoolcreate(int argc, char **argv)
     int config_len = 0;
     XLU_Config *config;
     const char *buf;
-    const char *name;
+    const char *name = NULL;
     uint32_t poolid;
     libxl_scheduler sched = 0;
     XLU_ConfigList *cpus;
@@ -7196,6 +7200,7 @@ out_cfg:
     xlu_cfg_destroy(config);
 out:
     free(config_data);
+    free((void*)name);
     return rc;
 }
 
author
#2 lore raw
Quoted 25 lines · Ian Campbell
Quoted 24 lines · Wei Liu

There are two invocations of libxl_basename, which returns a malloc'ed string. Those strings should be freed after used.

Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
---
 tools/libxl/xl_cmdimpl.c |    9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tools/libxl/xl_cmdimpl.c +1 -0
index 9afef3f..716a865 100644
@@ -920,6 +920,7 @@ static void parse_config_data(const char *config_source,
     int pci_permissive = 0;
     int pci_seize = 0;
     int i, e;
+    const char *basename;
 
     libxl_domain_create_info *c_info = &d_config->c_info;
     libxl_domain_build_info *b_info = &d_config->b_info;
@@ -1116,13 +1117,16 @@ static void parse_config_data(const char *config_source,
 
Quoted 16 lines

tools/libxl/xl_cmdimpl.c static void parse_config_data(const char *config_source, · line 1128

Quoted 12 lines
tools/libxl/xl_cmdimpl.c +3 -1
     switch(b_info->type) {
     case LIBXL_DOMAIN_TYPE_HVM:
-        if (!strcmp(libxl_basename(b_info->kernel), "hvmloader")) {
+        basename = libxl_basename(b_info->kernel);
+        if (!strcmp(basename, "hvmloader")) {
             fprintf(stderr, "WARNING: you seem to be using \"kernel\" "
                     "directive to override HVM guest firmware. Ignore "
                     "that. Use \"firmware_override\" instead if you "
                     "really want a non-default firmware\n");
             b_info->kernel = NULL;
         }
+        free((void*)basename);

I think you should un-const the declaration (in both cases) rather than adding casts.

Is it OK to change the declaration of a public API?

Wei.

Quoted 22 lines
Quoted 21 lines
tools/libxl/xl_cmdimpl.c +3 -1
+        basename = NULL;
 
         xlu_cfg_replace_string (config, "firmware_override",
                                 &b_info->u.hvm.firmware, 0);
@@ -7021,7 +7025,7 @@ int main_cpupoolcreate(int argc, char **argv)
     int config_len = 0;
     XLU_Config *config;
     const char *buf;
-    const char *name;
+    const char *name = NULL;
     uint32_t poolid;
     libxl_scheduler sched = 0;
     XLU_ConfigList *cpus;
@@ -7196,6 +7200,7 @@ out_cfg:
     xlu_cfg_destroy(config);
 out:
     free(config_data);
+    free((void*)name);
     return rc;
 }
 
#3 lore raw
Quoted 43 lines · Wei Liu
Quoted 25 lines · Ian Campbell
Quoted 24 lines · Wei Liu

There are two invocations of libxl_basename, which returns a malloc'ed string. Those strings should be freed after used.

Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
---
 tools/libxl/xl_cmdimpl.c |    9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tools/libxl/xl_cmdimpl.c +1 -0
index 9afef3f..716a865 100644
@@ -920,6 +920,7 @@ static void parse_config_data(const char *config_source,
     int pci_permissive = 0;
     int pci_seize = 0;
     int i, e;
+    const char *basename;
 
     libxl_domain_create_info *c_info = &d_config->c_info;
     libxl_domain_build_info *b_info = &d_config->b_info;
@@ -1116,13 +1117,16 @@ static void parse_config_data(const char *config_source,
 
Quoted 16 lines

tools/libxl/xl_cmdimpl.c static void parse_config_data(const char *config_source, · line 1128

Quoted 12 lines
tools/libxl/xl_cmdimpl.c +3 -1
     switch(b_info->type) {
     case LIBXL_DOMAIN_TYPE_HVM:
-        if (!strcmp(libxl_basename(b_info->kernel), "hvmloader")) {
+        basename = libxl_basename(b_info->kernel);
+        if (!strcmp(basename, "hvmloader")) {
             fprintf(stderr, "WARNING: you seem to be using \"kernel\" "
                     "directive to override HVM guest firmware. Ignore "
                     "that. Use \"firmware_override\" instead if you "
                     "really want a non-default firmware\n");
             b_info->kernel = NULL;
         }
+        free((void*)basename);

I think you should un-const the declaration (in both cases) rather than adding casts.

Quoted 1 line

Is it OK to change the declaration of a public API?

Oh god, libxl_basename returns a dynamically allocated string as a const. That's a bit mad... At least it is clearly documented as being a strdup'd thing!

I think we may need to use LIBXL_API_VERSION here to conditionally include the const. We've done that before see LIBXL_HAVE_NONCONST_EVENT_OCCURS_EVENT_ARG.

Ian.