]> xenbits.xensource.com Git - xen.git/commitdiff
tools: libxl: make it illegal to pass libxl__realloc(gc) a non-gc ptr
authorIan Campbell <ian.campbell@citrix.com>
Thu, 11 Feb 2016 09:23:54 +0000 (09:23 +0000)
committerIan Campbell <ian.campbell@citrix.com>
Thu, 11 Feb 2016 15:36:19 +0000 (15:36 +0000)
That is, if gc is not NOGC and ptr is not NULL then ptr must be
associated with a gc.

Currently in this case the new_ptr would not be registered with any
gc, which Coverity rightly points out (in various different places)
would be a memory leak.

It would also be possible to fix this by adding a libxl__ptr_add() at
the same point, however semantically it seems like a programming error
to gc-realloc a pointer which is not associated with the gc in
question, so treat it as such.

Compile tested only, this change could expose latent bugs.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
tools/libxl/libxl_internal.c
tools/libxl/libxl_internal.h

index 328046bc4f9fdbef717cf7b0f88408702e3f78f1..fc81130b8ef87f7b2293077ee7c9c01ae67781c4 100644 (file)
@@ -122,6 +122,10 @@ void *libxl__realloc(libxl__gc *gc, void *ptr, size_t new_size)
                 break;
             }
         }
+        if (i == gc->alloc_maxsize) {
+            LOG(CRITICAL, "pointer is not tracked by the given gc");
+            abort();
+        }
     }
 
     return new_ptr;
index fc1b558a6353bbf19ed0a488f7bf3ecefb6f640d..650a958489a6ff7ac6f3a7c96b2d461d4027c2f2 100644 (file)
@@ -617,7 +617,9 @@ _hidden void *libxl__zalloc(libxl__gc *gc_opt, size_t size) NN1;
 _hidden void *libxl__calloc(libxl__gc *gc_opt, size_t nmemb, size_t size) NN1;
 /* change the size of the memory block pointed to by @ptr to @new_size bytes.
  * unlike other allocation functions here any additional space between the
- * oldsize and @new_size is not initialised (similar to a gc'd realloc(3)). */
+ * oldsize and @new_size is not initialised (similar to a gc'd realloc(3)).
+ * if @ptr is non-NULL and @gc_opt is not nogc_gc then @ptr must have been
+ * registered with @gc_opt previously. */
 _hidden void *libxl__realloc(libxl__gc *gc_opt, void *ptr, size_t new_size) NN1;
 /* print @fmt into an allocated string large enoughto contain the result.
  * (similar to gc'd asprintf(3)). */