Skip to contentSkip to conversation

xl/libxl: fix API and two memory leaks

PATCH Applied Wei Liu 4 participants 12 messages Dec 1, 2014 – Dec 3, 2014

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

v2 cover xl/libxl: fix API and two memory leaks

Return value of libxl_basename was erroneously marked as "const". This series removes that "const" and fixes two memory leaks in xl.

I think these fixes should be included in 4.5, given that they fix real issues and are very straight foward to reason about.

Wei.

Wei Liu (2):
libxl: un-constify return value of libxl_basename
xl: fix two memory leaks

 tools/libxl/libxl.h       |   10 ++++++++++
 tools/libxl/libxl_utils.c |    5 ++++-
 tools/libxl/libxl_utils.h |    6 +++++-
 tools/libxl/xl_cmdimpl.c  |    8 ++++++--
 4 files changed, 25 insertions(+), 4 deletions(-)
Signature · 1 line
1.7.10.4
author
#1 lore raw

PATCH v2 1/2 libxl: un-constify return value of libxl_basename

The string returned is malloc'ed but marked as "const".

Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
---
 tools/libxl/libxl.h       |   10 ++++++++++
 tools/libxl/libxl_utils.c |    5 ++++-
 tools/libxl/libxl_utils.h |    6 +++++-
 3 files changed, 19 insertions(+), 2 deletions(-)
tools/libxl/libxl.h +10 -0
index 41d6e8d..291c190 100644
@@ -478,6 +478,16 @@ typedef struct libxl__ctx libxl_ctx;
 #endif
 
 /*
+ * LIBXL_HAVE_CONST_LIBXL_BASENAME_RETURN_VALUE
+ *
+ * The return value of libxl_basename is malloc'ed but the erroneously
+ * marked as "const" in releases before 4.5.
+ */
+#if defined(LIBXL_API_VERSION) && LIBXL_API_VERSION < 0x040500
+#define LIBXL_HAVE_CONST_LIBXL_BASENAME_RETURN_VALUE 1
+#endif
+
+/*
  * LIBXL_HAVE_PHYSINFO_OUTSTANDING_PAGES
  *
  * If this is defined, libxl_physinfo structure will contain an uint64 field
tools/libxl/libxl_utils.c +4 -1
index 3e1ba17..22119fc 100644
@@ -19,7 +19,10 @@
 
 #include "libxl_internal.h"
 
-const char *libxl_basename(const char *name)
+#ifdef LIBXL_HAVE_CONST_LIBXL_BASENAME_RETURN_VALUE
+const
+#endif
+char *libxl_basename(const char *name)
 {
     const char *filename;
     if (name == NULL)
tools/libxl/libxl_utils.h +5 -1
index 117b229..8277eb9 100644
@@ -18,7 +18,11 @@
 
 #include "libxl.h"
 
-const char *libxl_basename(const char *name); /* returns string from strdup */
+#ifdef LIBXL_HAVE_CONST_LIBXL_BASENAME_RETURN_VALUE
+const
+#endif
+char *libxl_basename(const char *name); /* returns string from strdup */
+
 unsigned long libxl_get_required_shadow_memory(unsigned long maxmem_kb, unsigned int smp_cpus);
 int libxl_name_to_domid(libxl_ctx *ctx, const char *name, uint32_t *domid);
 int libxl_domain_qualifier_to_domid(libxl_ctx *ctx, const char *name, uint32_t *domid);
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [PATCH v2 for-4.5 1/2] libxl: un-constify return value of libxl_basename

Quoted 17 lines · Wei Liu

The string returned is malloc'ed but marked as "const".

Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
---
 tools/libxl/libxl.h       |   10 ++++++++++
 tools/libxl/libxl_utils.c |    5 ++++-
 tools/libxl/libxl_utils.h |    6 +++++-
 3 files changed, 19 insertions(+), 2 deletions(-)
tools/libxl/libxl.h +0 -0
index 41d6e8d..291c190 100644
@@ -478,6 +478,16 @@ typedef struct libxl__ctx libxl_ctx;
 #endif

tools/libxl/libxl.h typedef struct libxl__ctx libxl_ctx; · line 488

Quoted 10 lines
tools/libxl/libxl.h +8 -0
 
 /*
+ * LIBXL_HAVE_CONST_LIBXL_BASENAME_RETURN_VALUE
+ *
+ * The return value of libxl_basename is malloc'ed but the erroneously
+ * marked as "const" in releases before 4.5.
+ */
+#if defined(LIBXL_API_VERSION) && LIBXL_API_VERSION < 0x040500
+#define LIBXL_HAVE_CONST_LIBXL_BASENAME_RETURN_VALUE 1
+#endif

This define is currently useless. Only newer code is capable of making use of newly introduced LIBXL_HAVE_$FOO flags, and with its current arrangement, this flag is only exposed to code requesting an older API version.

This instead needs to be LIBXL_HAVE_NONCONST_LIBXL_BASENAME_RETURN_VALUE which should be 1 for any API version >= 4.5

~Andrew

#3 lore raw

Re: [PATCH v2 for-4.5 1/2] libxl: un-constify return value of libxl_basename

Quoted 34 lines · Andrew Cooper
Quoted 17 lines · Wei Liu

The string returned is malloc'ed but marked as "const".

Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
---
 tools/libxl/libxl.h       |   10 ++++++++++
 tools/libxl/libxl_utils.c |    5 ++++-
 tools/libxl/libxl_utils.h |    6 +++++-
 3 files changed, 19 insertions(+), 2 deletions(-)
tools/libxl/libxl.h +0 -0
index 41d6e8d..291c190 100644
@@ -478,6 +478,16 @@ typedef struct libxl__ctx libxl_ctx;
 #endif

tools/libxl/libxl.h typedef struct libxl__ctx libxl_ctx; · line 488

Quoted 10 lines
tools/libxl/libxl.h +8 -0
 
 /*
+ * LIBXL_HAVE_CONST_LIBXL_BASENAME_RETURN_VALUE
+ *
+ * The return value of libxl_basename is malloc'ed but the erroneously
+ * marked as "const" in releases before 4.5.
+ */
+#if defined(LIBXL_API_VERSION) && LIBXL_API_VERSION < 0x040500
+#define LIBXL_HAVE_CONST_LIBXL_BASENAME_RETURN_VALUE 1
+#endif

This define is currently useless. Only newer code is capable of making use of newly introduced LIBXL_HAVE_$FOO flags, and with its current arrangement, this flag is only exposed to code requesting an older API version.

Quoted 2 lines

This instead needs to be LIBXL_HAVE_NONCONST_LIBXL_BASENAME_RETURN_VALUE which should be 1 for any API version >= 4.5

Oops, yes. Wei, can you send an incremental fixup please?

Ian.

author
#4 lore raw

PATCH v2 2/2 xl: fix two memory leaks

In reply to Wei Liu

Free strings returned by libxl_basename after used.

Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
---
 tools/libxl/xl_cmdimpl.c |    8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/libxl/xl_cmdimpl.c +6 -2
index 0e754e7..fe3034f 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;
+    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,15 @@ 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(basename);
 
         xlu_cfg_replace_string (config, "firmware_override",
                                 &b_info->u.hvm.firmware, 0);
@@ -7023,7 +7026,7 @@ int main_cpupoolcreate(int argc, char **argv)
     int config_len = 0;
     XLU_Config *config;
     const char *buf;
-    const char *name;
+    char *name = NULL;
     uint32_t poolid;
     libxl_scheduler sched = 0;
     XLU_ConfigList *cpus;
@@ -7197,6 +7200,7 @@ int main_cpupoolcreate(int argc, char **argv)
 out_cfg:
     xlu_cfg_destroy(config);
 out:
+    free(name);
     free(config_data);
     return rc;
 }
Signature · 1 line
1.7.10.4
#5 lore raw

Re: [PATCH v2 for-4.5 2/2] xl: fix two memory leaks

Quoted 14 lines · Wei Liu

Free strings returned by libxl_basename after used.

Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
---
 tools/libxl/xl_cmdimpl.c |    8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/libxl/xl_cmdimpl.c +0 -0
index 0e754e7..fe3034f 100644

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

Quoted 4 lines
tools/libxl/xl_cmdimpl.c +1 -0
@@ -920,6 +920,7 @@ static void parse_config_data(const char *config_source,
     int pci_permissive = 0;
     int pci_seize = 0;
     int i, e;
+    char *basename;

For some reason on arm32 (only) this causes:
xl_cmdimpl.c: In function ‘parse_config_data’:
xl_cmdimpl.c:929:11: error: declaration of ‘basename’ shadows a global declaration [-Werror=shadow]

basename(3) is defined by libgen.h which we don't include, so I suspect this is a libc issue on armhf (unless Ian has any other ideas?).

How about I s/basename/kernel_basename/ or some such on commit?

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#6 lore raw

Re: [PATCH v2 for-4.5 2/2] xl: fix two memory leaks

Quoted 24 lines · Ian Campbell
Quoted 14 lines · Wei Liu

Free strings returned by libxl_basename after used.

Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
---
 tools/libxl/xl_cmdimpl.c |    8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/libxl/xl_cmdimpl.c +0 -0
index 0e754e7..fe3034f 100644

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

Quoted 4 lines
tools/libxl/xl_cmdimpl.c +1 -0
@@ -920,6 +920,7 @@ static void parse_config_data(const char *config_source,
     int pci_permissive = 0;
     int pci_seize = 0;
     int i, e;
+    char *basename;

For some reason on arm32 (only) this causes:
xl_cmdimpl.c: In function ‘parse_config_data’:
xl_cmdimpl.c:929:11: error: declaration of ‘basename’ shadows a global declaration [-Werror=shadow]

Quoted 5 lines

basename(3) is defined by libgen.h which we don't include, so I suspect this is a libc issue on armhf (unless Ian has any other ideas?).

How about I s/basename/kernel_basename/ or some such on commit?

That's OK.

Wei.

Quoted 1 line

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

Acked-by on the v2 series
#7 lore raw
In reply to Wei Liu
Quoted 5 lines · Wei Liu

Return value of libxl_basename was erroneously marked as "const". This series removes that "const" and fixes two memory leaks in xl.

I think these fixes should be included in 4.5, given that they fix real issues and are very straight foward to reason about.

Agreed. Both patches:

I've added the CCs to this intro mail...

Did you by any chance look for other const char * return values?

Ian.

author
#8 lore raw
Quoted 7 lines · Ian Campbell
Quoted 5 lines · Wei Liu

Return value of libxl_basename was erroneously marked as "const". This series removes that "const" and fixes two memory leaks in xl.

I think these fixes should be included in 4.5, given that they fix real issues and are very straight foward to reason about.

Quoted 7 lines

Agreed. Both patches:

I've added the CCs to this intro mail...

Did you by any chance look for other const char * return values?

I've looked at other public API headers. I didn't spot other obvious problems with regard to const char * return values.

Wei.

Quoted 1 line

Ian.

#9 lore raw
Quoted 11 lines · Wei Liu
Quoted 10 lines · Ian Campbell
Quoted 5 lines · Wei Liu

Return value of libxl_basename was erroneously marked as "const". This series removes that "const" and fixes two memory leaks in xl.

I think these fixes should be included in 4.5, given that they fix real issues and are very straight foward to reason about.

Agreed. Both patches:

Quoted 7 lines
Quoted 4 lines

I've added the CCs to this intro mail...

Did you by any chance look for other const char * return values?

I've looked at other public API headers. I didn't spot other obvious problems with regard to const char * return values.

Super, thanks!

#10 lore raw
In reply to Wei Liu
Quoted 5 lines · Wei Liu

Return value of libxl_basename was erroneously marked as "const". This series removes that "const" and fixes two memory leaks in xl.

I think these fixes should be included in 4.5, given that they fix real issues and are very straight foward to reason about.

Thank you for catching those!

Quoted 16 lines

Wei.

Wei Liu (2):
libxl: un-constify return value of libxl_basename
xl: fix two memory leaks

 tools/libxl/libxl.h       |   10 ++++++++++
 tools/libxl/libxl_utils.c |    5 ++++-
 tools/libxl/libxl_utils.h |    6 +++++-
 tools/libxl/xl_cmdimpl.c  |    8 ++++++--
 4 files changed, 25 insertions(+), 4 deletions(-)
Signature · 1 line
1.7.10.4
Applied
#11 lore raw
Quoted 10 lines · Konrad Rzeszutek Wilk
Quoted 5 lines · Wei Liu

Return value of libxl_basename was erroneously marked as "const". This series removes that "const" and fixes two memory leaks in xl.

I think these fixes should be included in 4.5, given that they fix real issues and are very straight foward to reason about.

Thank you for catching those!

Applied both, including the modification discussed in my reply to the second.

Quoted 21 lines
Quoted 7 lines

Wei.

Wei Liu (2):
libxl: un-constify return value of libxl_basename
xl: fix two memory leaks

Quoted 9 lines
 tools/libxl/libxl.h       |   10 ++++++++++
 tools/libxl/libxl_utils.c |    5 ++++-
 tools/libxl/libxl_utils.h |    6 +++++-
 tools/libxl/xl_cmdimpl.c  |    8 ++++++--
 4 files changed, 25 insertions(+), 4 deletions(-)
Signature · 1 line
1.7.10.4

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel