libxl: Use libxl_strdup instead of strdup on libxl_version_info
PATCH 1/1 libxl: Use libxl_strdup instead of strdup on libxl_version_info
As the libxl_strdup allows us to unwind and free all of the allocations, while strdup requires the callers to remember to free (which they didn't seem too).
--- tools/libxl/libxl.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
tools/libxl/libxl.c +8 -8
@@ -5282,19 +5282,19 @@ const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx) info->xen_version_minor = xen_version &0xFF ;xc_version (ctx->xch, XENVER_extraversion, &u.xen_extra); - info->xen_version_extra =strdup (u.xen_extra); + info->xen_version_extra =libxl__strdup (NOGC , u.xen_extra);xc_version (ctx->xch, XENVER_compile_info, &u.xen_cc); - info->compiler =strdup (u.xen_cc.compiler); - info->compile_by =strdup (u.xen_cc.compile_by); - info->compile_domain =strdup (u.xen_cc.compile_domain); - info->compile_date =strdup (u.xen_cc.compile_date); + info->compiler =libxl__strdup (NOGC , u.xen_cc.compiler); + info->compile_by =libxl__strdup (NOGC , u.xen_cc.compile_by); + info->compile_domain =libxl__strdup (NOGC , u.xen_cc.compile_domain); + info->compile_date =libxl__strdup (NOGC , u.xen_cc.compile_date);xc_version (ctx->xch, XENVER_capabilities, &u.xen_caps); - info->capabilities =strdup (u.xen_caps); + info->capabilities =libxl__strdup (NOGC , u.xen_caps);xc_version (ctx->xch, XENVER_changeset, &u.xen_chgset); - info->changeset =strdup (u.xen_chgset); + info->changeset =libxl__strdup (NOGC , u.xen_chgset);xc_version (ctx->xch, XENVER_platform_parameters, &u.p_parms); info->virt_start = u.p_parms.virt_start;
@@ -5302,7 +5302,7 @@ const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx) info->pagesize =xc_version (ctx->xch, XENVER_pagesize,NULL );xc_version (ctx->xch, XENVER_commandline, &u.xen_commandline); - info->commandline =strdup (u.xen_commandline); + info->commandline =libxl__strdup (NOGC , u.xen_commandline);return info; }
Signature · 1 line
2.4.3
Quoted 4 lines · Konrad Rzeszutek Wilk
As the
libxl_strdupallows us to unwind and free all of the allocations, while strdup requires the callers to remember to free (which they didn't seem too).
Grrrrr.. Ignore it pls. I cherry picked it and had a preceding patch that added the GC_INIT which this patch missed.
(And also an GC_FREE).
Quoted 49 lines
Suggested-by Wei Liu <[email protected]>Signed-off-by Konrad Rzeszutek Wilk <[email protected]>---
tools/libxl/libxl.c| 16 ++++++++--------1 file changed, 8 insertions(+), 8 deletions(-)tools/libxl/libxl.c +7
-7@@ -5282,19 +5282,19 @@ const libxl_version_info*libxl_get_version_info (libxl_ctx *ctx) info->xen_version_minor = xen_version & 0xFF;xc_version (ctx->xch, XENVER_extraversion, &u.xen_extra); - info->xen_version_extra =strdup (u.xen_extra); + info->xen_version_extra =libxl__strdup (NOGC , u.xen_extra);xc_version (ctx->xch, XENVER_compile_info, &u.xen_cc); - info->compiler =strdup (u.xen_cc.compiler); - info->compile_by =strdup (u.xen_cc.compile_by); - info->compile_domain =strdup (u.xen_cc.compile_domain); - info->compile_date =strdup (u.xen_cc.compile_date); + info->compiler =libxl__strdup (NOGC , u.xen_cc.compiler); + info->compile_by =libxl__strdup (NOGC , u.xen_cc.compile_by); + info->compile_domain =libxl__strdup (NOGC , u.xen_cc.compile_domain); + info->compile_date =libxl__strdup (NOGC , u.xen_cc.compile_date);xc_version (ctx->xch, XENVER_capabilities, &u.xen_caps); - info->capabilities =strdup (u.xen_caps); + info->capabilities =libxl__strdup (NOGC , u.xen_caps);xc_version (ctx->xch, XENVER_changeset, &u.xen_chgset); - info->changeset =strdup (u.xen_chgset); + info->changeset =libxl__strdup (NOGC , u.xen_chgset);xc_version(ctx->xch, XENVER_platform_parameters, &u.p_parms); info->virt_start = u.p_parms.virt_start;tools/libxl/libxl.c +1
-1@@ -5302,7 +5302,7 @@ const libxl_version_info*libxl_get_version_info (libxl_ctx *ctx) info->pagesize = xc_version(ctx->xch, XENVER_pagesize, NULL);xc_version (ctx->xch, XENVER_commandline, &u.xen_commandline); - info->commandline =strdup (u.xen_commandline); + info->commandline =libxl__strdup (NOGC , u.xen_commandline);return info;}
Quoted 8 lines · Konrad Rzeszutek Wilk
Quoted 4 lines · Konrad Rzeszutek Wilk
As the
libxl_strdupallows us to unwind and free all of the allocations, while strdup requires the callers to remember to free (which they didn't seem too).
Quoted 4 lines
Grrrrr.. Ignore it pls. I cherry picked it and had a preceding patch that added the
GC_INITwhich this patch missed.(And also an
GC_FREE).
Allocating these returned values via a gc would be wrong per the comment about memory management at the top of libxl.h, where these fall into category (b) and require the caller to free. They should normally do so by calling libxl_version_info_dispose(), not manually (which would be quite tedious and error prone).
Returning GC'd values to the application would be wrong since they would be freed by the GC_FREE before return upon exit from libxl.
However you've used NOGC, which compared to raw strdup etc only has the extra behaviour of abort-on-alloc-fail and not any "unwind and free all" behaviour which the commit message mentions. So this would be a good change, in that it improves error handling, rather than for any of the reasons mentioned in the commit message.
WRT freeing the results, normally the caller would need to do so by calling the appropriate _dispose() function. However libxl_get_version_info() is special and returns a cached result from the ctx which cannot and should not be freed (as evidenced by it returning a const struct). This data is freed in libxl_ctx_free() by calling libxl_version_info_dispose(). This is why none of the callers remember to free -- they shouldn't be doing so.
Ian.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel