diff options
author | Ian Jackson <ian.jackson@eu.citrix.com> | 2012-06-28 18:43:25 +0100 |
---|---|---|
committer | Ian Jackson <ian.jackson@eu.citrix.com> | 2012-06-28 18:43:25 +0100 |
commit | 27e1ccd1db641b4f57c8249a6e4d7492140dd285 (patch) | |
tree | d7b36cf04a52d929987f26afba33a5dbf8598995 /tools/libxl/libxl_internal.c | |
parent | 145511d2ce456a6590f28e14e32b7c4cce1c4c69 (diff) | |
download | xen-27e1ccd1db641b4f57c8249a6e4d7492140dd285.tar.gz xen-27e1ccd1db641b4f57c8249a6e4d7492140dd285.tar.bz2 xen-27e1ccd1db641b4f57c8249a6e4d7492140dd285.zip |
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 <bjzhang@suse.com>
Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
Cc: Bamvor Jian Zhang <bjzhang@suse.com>
Acked-by: Ian Campbell <Ian.Campbell@citrix.com>
Committed-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
Diffstat (limited to 'tools/libxl/libxl_internal.c')
-rw-r--r-- | tools/libxl/libxl_internal.c | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/tools/libxl/libxl_internal.c b/tools/libxl/libxl_internal.c index 8139520b78..fbff7d06d9 100644 --- a/tools/libxl/libxl_internal.c +++ b/tools/libxl/libxl_internal.c @@ -30,11 +30,16 @@ void libxl__alloc_failed(libxl_ctx *ctx, const char *func, #undef L } +static int gc_is_real(const libxl__gc *gc) +{ + return gc->alloc_maxsize >= 0; +} + void libxl__ptr_add(libxl__gc *gc, void *ptr) { int i; - if (!gc) + if (!gc_is_real(gc)) return; if (!ptr) @@ -66,6 +71,8 @@ void libxl__free_all(libxl__gc *gc) void *ptr; int i; + assert(gc_is_real(gc)); + for (i = 0; i < gc->alloc_maxsize; i++) { ptr = gc->alloc_ptrs[i]; gc->alloc_ptrs[i] = NULL; @@ -104,7 +111,7 @@ void *libxl__realloc(libxl__gc *gc, void *ptr, size_t new_size) if (ptr == NULL) { libxl__ptr_add(gc, new_ptr); - } else if (new_ptr != ptr && gc != NULL) { + } else if (new_ptr != ptr && gc_is_real(gc)) { for (i = 0; i < gc->alloc_maxsize; i++) { if (gc->alloc_ptrs[i] == ptr) { gc->alloc_ptrs[i] = new_ptr; |