From 9e5c4389a8abff06b6f2082b4528c086fc59236c Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Mon, 24 Aug 2015 10:17:55 +0200 Subject: [PATCH] Add checks for failure of an allocation function from libxml (#8070) g_malloc always trap on allocation errors but not xmlMalloc. --- tests/tests.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/tests.c b/tests/tests.c index 606f5bbe..6c55b490 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -71,6 +71,38 @@ fail_logger(const gchar *log_domain, GLogLevelFlags log_level, " «%d»", message, log_domain, log_level); } +static xmlFreeFunc free_func; +static xmlMallocFunc malloc_func; +static xmlReallocFunc realloc_func; +static xmlStrdupFunc strdup_func; + +void * +my_malloc(size_t size) +{ + void *ptr = malloc_func(size); + if (! ptr) { + fail("xmlMalloc failed"); + } + return ptr; +} + +void * +my_realloc(void *mem, size_t size) +{ + void *ptr = realloc_func(mem, size); + if (! ptr) { + fail("xmlRealloc failed"); + } + return ptr; +} + +void +setup_xml_mem_allocation() +{ + xmlMemGet(&free_func, &malloc_func, &realloc_func, &strdup_func); + xmlMemSetup(free_func, my_malloc, my_realloc, strdup_func); +} + int main(int argc, char *argv[]) { @@ -79,6 +111,7 @@ main(int argc, char *argv[]) int i; int dont_fork = 0; + setup_xml_mem_allocation(); for (i=1; i