From da93b3992a1ede2ecea0662f588cc970362ba64f Mon Sep 17 00:00:00 2001 From: Rhodri James Date: Wed, 8 Feb 2017 15:26:18 +0000 Subject: [PATCH] Refactor allocator test support --- expat/tests/runtests.c | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/expat/tests/runtests.c b/expat/tests/runtests.c index a5c3ed55..bb083645 100644 --- a/expat/tests/runtests.c +++ b/expat/tests/runtests.c @@ -2812,17 +2812,32 @@ START_TEST(test_ns_parser_reset) END_TEST /* Control variable; the number of times duff_allocator() will successfully allocate */ -static unsigned int allocation_count = 0; +#define ALLOC_ALWAYS_SUCCEED (-1) +#define REALLOC_ALWAYS_SUCCEED (-1) + +static int allocation_count = ALLOC_ALWAYS_SUCCEED; +static int reallocation_count = REALLOC_ALWAYS_SUCCEED; /* Crocked allocator for allocation failure tests */ static void *duff_allocator(size_t size) { if (allocation_count == 0) return NULL; - allocation_count--; + if (allocation_count != ALLOC_ALWAYS_SUCCEED) + allocation_count--; return malloc(size); } +/* Crocked reallocator for allocation failure tests */ +static void *duff_reallocator(void *ptr, size_t size) +{ + if (reallocation_count == 0) + return NULL; + if (reallocation_count != REALLOC_ALWAYS_SUCCEED) + reallocation_count--; + return realloc(ptr, size); +} + /* Test that a failure to allocate the parser structure fails gracefully */ START_TEST(test_misc_alloc_create_parser) { @@ -2878,7 +2893,7 @@ START_TEST(test_misc_alloc_ns) int repeated = 0; XML_Char ns_sep[2] = { ' ', '\0' }; - allocation_count = 10000; + allocation_count = ALLOC_ALWAYS_SUCCEED; parser = XML_ParserCreate_MM(NULL, &memsuite, ns_sep); if (parser == NULL) { fail("Parser not created"); @@ -2917,7 +2932,7 @@ START_TEST(test_misc_alloc_ns_parse_buffer) void *buffer; /* Make sure the basic parser is allocated */ - allocation_count = 10000; + allocation_count = ALLOC_ALWAYS_SUCCEED; parser = XML_ParserCreate_MM(NULL, &memsuite, ns_sep); if (parser == NULL) fail("Parser not created"); @@ -2931,7 +2946,7 @@ START_TEST(test_misc_alloc_ns_parse_buffer) fail("Pre-init XML_ParseBuffer faulted for wrong reason"); /* Now with actual memory allocation */ - allocation_count = 10000; + allocation_count = ALLOC_ALWAYS_SUCCEED; if (XML_ParseBuffer(parser, 0, XML_FALSE) != XML_STATUS_OK) xml_failure(parser); @@ -3056,10 +3071,15 @@ END_TEST static void alloc_setup(void) { - XML_Memory_Handling_Suite memsuite = { duff_allocator, realloc, free }; + XML_Memory_Handling_Suite memsuite = { + duff_allocator, + duff_reallocator, + free + }; /* Ensure the parser creation will go through */ - allocation_count = 10000; + allocation_count = ALLOC_ALWAYS_SUCCEED; + reallocation_count = REALLOC_ALWAYS_SUCCEED; parser = XML_ParserCreate_MM(NULL, &memsuite, NULL); if (parser == NULL) fail("Parser not created"); @@ -3098,7 +3118,7 @@ external_entity_duff_loader(XML_Parser parser, fail("Extern parser not created with allocation count 10"); /* Make sure other random allocation doesn't now fail */ - allocation_count = 10000; + allocation_count = ALLOC_ALWAYS_SUCCEED; /* Make sure the failure code path is executed too */ return XML_STATUS_ERROR; @@ -3215,7 +3235,7 @@ external_entity_dbl_handler(XML_Parser parser, } } - allocation_count = 10000; + allocation_count = ALLOC_ALWAYS_SUCCEED; if (_XML_Parse_SINGLE_BYTES(new_parser, text, strlen(text), XML_TRUE) == XML_STATUS_ERROR) { xml_failure(new_parser); return 0; @@ -3280,7 +3300,7 @@ external_entity_dbl_handler_2(XML_Parser parser, } /* Ensure future allocations will be well */ - allocation_count = 10000; + allocation_count = ALLOC_ALWAYS_SUCCEED; if (i == 0) { fail("first external parser unexpectedly created"); XML_ParserFree(new_parser); @@ -3294,7 +3314,7 @@ external_entity_dbl_handler_2(XML_Parser parser, /* Just run through once */ text = ("" ""); - allocation_count = 10000; + allocation_count = ALLOC_ALWAYS_SUCCEED; new_parser = XML_ExternalEntityParserCreate(parser, context, NULL); if (new_parser == NULL) { fail("Unable to create second external parser"); -- 2.40.0