Ian Campbell
2011-Sep-21 13:55 UTC
[Xen-devel] [PATCH] libxl: correct allocation size in libxl_list_vm
# HG changeset patch # User Ian Campbell <ian.campbell@citrix.com> # Date 1316609964 -3600 # Node ID 4a6d34dffcf9d568a7830ee6de07a581c57e7342 # Parent b11af4a5cdc6a94e41a81d456f07b4d70cdb5ffe libxl: correct allocation size in libxl_list_vm *ptr has type libxl_vminfo not libxl_domid, so correct calloc call. This the second instance of this bug I''ve noticed recently, I did a quick audit of other similar uses of sizeof(...) and all I spotted were a couple of harmlessly reversed calloc arguments. It''s a pretty strong argument for "foo = ..alloc(sizeof(*foo))" rather than "alloc(sizeof(foos_type))" though... Signed-off-by: Ian Campbell <ian.campbell@citrix.com> diff -r b11af4a5cdc6 -r 4a6d34dffcf9 tools/libxl/libxl.c --- a/tools/libxl/libxl.c Wed Sep 21 13:59:24 2011 +0100 +++ b/tools/libxl/libxl.c Wed Sep 21 13:59:24 2011 +0100 @@ -449,7 +449,7 @@ libxl_vminfo * libxl_list_vm(libxl_ctx * xc_domaininfo_t info[1024]; int size = 1024; - ptr = calloc(size, sizeof(libxl_dominfo)); + ptr = calloc(size, sizeof(libxl_vminfo)); if (!ptr) return NULL; diff -r b11af4a5cdc6 -r 4a6d34dffcf9 tools/libxl/libxl_dm.c --- a/tools/libxl/libxl_dm.c Wed Sep 21 13:59:24 2011 +0100 +++ b/tools/libxl/libxl_dm.c Wed Sep 21 13:59:24 2011 +0100 @@ -778,7 +778,7 @@ retry_transaction: libxl_domain_unpause(ctx, domid); if (starting_r) { - *starting_r = calloc(sizeof(libxl__device_model_starting), 1); + *starting_r = calloc(1, sizeof(libxl__device_model_starting)); (*starting_r)->domid = info->domid; (*starting_r)->dom_path = libxl__xs_get_dompath(gc, info->domid); (*starting_r)->for_spawn = NULL; @@ -855,11 +855,11 @@ int libxl__create_device_model(libxl__gc if (starting_r) { rc = ERROR_NOMEM; - *starting_r = calloc(sizeof(libxl__device_model_starting), 1); + *starting_r = calloc(1, sizeof(libxl__device_model_starting)); if (!*starting_r) goto out_close; p = *starting_r; - p->for_spawn = calloc(sizeof(libxl__spawn_starting), 1); + p->for_spawn = calloc(1, sizeof(libxl__spawn_starting)); } else { p = &buf_starting; p->for_spawn = NULL; _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
Ian Jackson
2011-Sep-28 15:34 UTC
Re: [Xen-devel] [PATCH] libxl: correct allocation size in libxl_list_vm
Ian Campbell writes ("[Xen-devel] [PATCH] libxl: correct allocation size in libxl_list_vm"):> libxl: correct allocation size in libxl_list_vmAcked-by: Ian Jackson <ian.jackson@eu.citrix.com> Committed-by: Ian Jackson <ian.jackson@eu.citrix.com>> *ptr has type libxl_vminfo not libxl_domid, so correct calloc call. > > This the second instance of this bug I''ve noticed recently, I did a > quick audit of other similar uses of sizeof(...) and all I spotted > were a couple of harmlessly reversed calloc arguments. It''s a pretty > strong argument for "foo = ..alloc(sizeof(*foo))" rather than > "alloc(sizeof(foos_type))" though...The correct approach to this is to make a macro along these lines: #define OUR_CALLOC(foo) ((foo)=calloc(sizeof(*(foo)))) I think we may have some of these but we should have a complete set. Ian. _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel