From 27e1ccd1db641b4f57c8249a6e4d7492140dd285 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 28 Jun 2012 18:43:25 +0100 Subject: libxl: Do not pass NULL as gc_opt; introduce NOGC In 25182:6c3345d7e9d9 the practice of passing NULL to gc-using memory allocation functions was introduced. However, the arrangements there were not correct as committed, because the error handling and logging depends on getting a ctx from the gc - so an allocation error would in fact result in libxl dereferencing NULL. Instead, provide a special dummy gc in the ctx, called `nogc_gc'. It is marked out specially by having alloc_maxsize==-1, which is otherwise invalid. Functions which need to actually look into the gc use the new test function gc_is_real (whose purpose is mainly clarity of the code) to check whether the gc is the dummy one, and do nothing if it is. And we provide a helper macro NOGC which uses the in-scope real gc to find the ctx and hence the dummy gc (and which replaces the previous #define NOGC NULL). Change all callers which pass 0 or NULL to an allocation function to use NOGC or &ctx->nogc_gc, as applicable in the context. We add a comment near the definition of LIBXL_INIT_GC pointing out that it isn't any more the only place a libxl__gc struct is initialised, for the benefit of anyone changing the contents of gc's in the future. Also, actually document that libxl__ptr_add is legal with ptr==NULL, and change a couple of calls not to check for NULL argument. Reported-by: Bamvor Jian Zhang Signed-off-by: Ian Jackson Cc: Bamvor Jian Zhang Acked-by: Ian Campbell Committed-by: Ian Jackson --- tools/libxl/libxl_utils.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'tools/libxl/libxl_utils.c') diff --git a/tools/libxl/libxl_utils.c b/tools/libxl/libxl_utils.c index 707306213c..f69a24c267 100644 --- a/tools/libxl/libxl_utils.c +++ b/tools/libxl/libxl_utils.c @@ -58,8 +58,7 @@ char *libxl_domid_to_name(libxl_ctx *ctx, uint32_t domid) char *libxl__domid_to_name(libxl__gc *gc, uint32_t domid) { char *s = libxl_domid_to_name(libxl__gc_owner(gc), domid); - if ( s ) - libxl__ptr_add(gc, s); + libxl__ptr_add(gc, s); return s; } @@ -107,8 +106,7 @@ char *libxl_cpupoolid_to_name(libxl_ctx *ctx, uint32_t poolid) char *libxl__cpupoolid_to_name(libxl__gc *gc, uint32_t poolid) { char *s = libxl_cpupoolid_to_name(libxl__gc_owner(gc), poolid); - if ( s ) - libxl__ptr_add(gc, s); + libxl__ptr_add(gc, s); return s; } -- cgit v1.2.3