1From 43992e4ae25fc3dc0eec0cd3a29313555d56aee2 Mon Sep 17 00:00:00 2001 2From: Sebastian Pipping <sebastian@pipping.org> 3Date: Mon, 19 Sep 2022 18:16:15 +0200 4Subject: [PATCH] tests: Cover overeager DTD destruction in 5 XML_ExternalEntityParserCreate 6 7--- 8 tests/runtests.c | 49 ++++++++++++++++++++++++++++++++++++++++++ 9 1 file changed, 49 insertions(+) 10 11diff --git a/tests/runtests.c b/tests/runtests.c 12index 245fe9bda..acb744dd4 100644 13--- a/tests/runtests.c 14+++ b/tests/runtests.c 15@@ -10208,6 +10208,53 @@ START_TEST(test_alloc_long_notation) { 16 } 17 END_TEST 18 19+static int XMLCALL 20+external_entity_parser_create_alloc_fail_handler(XML_Parser parser, 21+ const XML_Char *context, 22+ const XML_Char *base, 23+ const XML_Char *systemId, 24+ const XML_Char *publicId) { 25+ UNUSED_P(base); 26+ UNUSED_P(systemId); 27+ UNUSED_P(publicId); 28+ 29+ if (context != NULL) 30+ fail("Unexpected non-NULL context"); 31+ 32+ // The following number intends to fail the upcoming allocation in line 33+ // "parser->m_protocolEncodingName = copyString(encodingName, 34+ // &(parser->m_mem));" in function parserInit. 35+ allocation_count = 3; 36+ 37+ const XML_Char *const encodingName = XCS("UTF-8"); // needs something non-NULL 38+ const XML_Parser ext_parser 39+ = XML_ExternalEntityParserCreate(parser, context, encodingName); 40+ if (ext_parser != NULL) 41+ fail( 42+ "Call to XML_ExternalEntityParserCreate was expected to fail out-of-memory"); 43+ 44+ allocation_count = ALLOC_ALWAYS_SUCCEED; 45+ return XML_STATUS_ERROR; 46+} 47+ 48+START_TEST(test_alloc_reset_after_external_entity_parser_create_fail) { 49+ const char *const text = "<!DOCTYPE doc SYSTEM 'foo'><doc/>"; 50+ 51+ XML_SetExternalEntityRefHandler( 52+ g_parser, external_entity_parser_create_alloc_fail_handler); 53+ XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS); 54+ 55+ if (XML_Parse(g_parser, text, (int)strlen(text), XML_TRUE) 56+ != XML_STATUS_ERROR) 57+ fail("Call to parse was expected to fail"); 58+ 59+ if (XML_GetErrorCode(g_parser) != XML_ERROR_EXTERNAL_ENTITY_HANDLING) 60+ fail("Call to parse was expected to fail from the external entity handler"); 61+ 62+ XML_ParserReset(g_parser, NULL); 63+} 64+END_TEST 65+ 66 static void 67 nsalloc_setup(void) { 68 XML_Memory_Handling_Suite memsuite = {duff_allocator, duff_reallocator, free}; 69@@ -12401,6 +12448,8 @@ make_suite(void) { 70 tcase_add_test(tc_alloc, test_alloc_long_public_id); 71 tcase_add_test(tc_alloc, test_alloc_long_entity_value); 72 tcase_add_test(tc_alloc, test_alloc_long_notation); 73+ tcase_add_test__ifdef_xml_dtd( 74+ tc_alloc, test_alloc_reset_after_external_entity_parser_create_fail); 75 76 suite_add_tcase(s, tc_nsalloc); 77 tcase_add_checked_fixture(tc_nsalloc, nsalloc_setup, nsalloc_teardown); 78