misc_tests.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /* Tests in the "miscellaneous" test case for the Expat test suite
  2. __ __ _
  3. ___\ \/ /_ __ __ _| |_
  4. / _ \\ /| '_ \ / _` | __|
  5. | __// \| |_) | (_| | |_
  6. \___/_/\_\ .__/ \__,_|\__|
  7. |_| XML parser
  8. Copyright (c) 2001-2006 Fred L. Drake, Jr. <[email protected]>
  9. Copyright (c) 2003 Greg Stein <[email protected]>
  10. Copyright (c) 2005-2007 Steven Solie <[email protected]>
  11. Copyright (c) 2005-2012 Karl Waclawek <[email protected]>
  12. Copyright (c) 2016-2025 Sebastian Pipping <[email protected]>
  13. Copyright (c) 2017-2022 Rhodri James <[email protected]>
  14. Copyright (c) 2017 Joe Orton <[email protected]>
  15. Copyright (c) 2017 José Gutiérrez de la Concha <[email protected]>
  16. Copyright (c) 2018 Marco Maggi <[email protected]>
  17. Copyright (c) 2019 David Loffredo <[email protected]>
  18. Copyright (c) 2020 Tim Gates <[email protected]>
  19. Copyright (c) 2021 Donghee Na <[email protected]>
  20. Copyright (c) 2023 Sony Corporation / Snild Dolkow <[email protected]>
  21. Licensed under the MIT license:
  22. Permission is hereby granted, free of charge, to any person obtaining
  23. a copy of this software and associated documentation files (the
  24. "Software"), to deal in the Software without restriction, including
  25. without limitation the rights to use, copy, modify, merge, publish,
  26. distribute, sublicense, and/or sell copies of the Software, and to permit
  27. persons to whom the Software is furnished to do so, subject to the
  28. following conditions:
  29. The above copyright notice and this permission notice shall be included
  30. in all copies or substantial portions of the Software.
  31. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  32. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  33. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  34. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  35. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  36. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  37. USE OR OTHER DEALINGS IN THE SOFTWARE.
  38. */
  39. #if defined(NDEBUG)
  40. # undef NDEBUG /* because test suite relies on assert(...) at the moment */
  41. #endif
  42. #include <assert.h>
  43. #include <string.h>
  44. #include "expat_config.h"
  45. #include "expat.h"
  46. #include "internal.h"
  47. #include "minicheck.h"
  48. #include "memcheck.h"
  49. #include "common.h"
  50. #include "ascii.h" /* for ASCII_xxx */
  51. #include "handlers.h"
  52. #include "misc_tests.h"
  53. void XMLCALL accumulate_characters_ext_handler(void *userData,
  54. const XML_Char *s, int len);
  55. /* Test that a failure to allocate the parser structure fails gracefully */
  56. START_TEST(test_misc_alloc_create_parser) {
  57. XML_Memory_Handling_Suite memsuite = {duff_allocator, realloc, free};
  58. unsigned int i;
  59. const unsigned int max_alloc_count = 10;
  60. /* Something this simple shouldn't need more than 10 allocations */
  61. for (i = 0; i < max_alloc_count; i++) {
  62. g_allocation_count = i;
  63. g_parser = XML_ParserCreate_MM(NULL, &memsuite, NULL);
  64. if (g_parser != NULL)
  65. break;
  66. }
  67. if (i == 0)
  68. fail("Parser unexpectedly ignored failing allocator");
  69. else if (i == max_alloc_count)
  70. fail("Parser not created with max allocation count");
  71. }
  72. END_TEST
  73. /* Test memory allocation failures for a parser with an encoding */
  74. START_TEST(test_misc_alloc_create_parser_with_encoding) {
  75. XML_Memory_Handling_Suite memsuite = {duff_allocator, realloc, free};
  76. unsigned int i;
  77. const unsigned int max_alloc_count = 10;
  78. /* Try several levels of allocation */
  79. for (i = 0; i < max_alloc_count; i++) {
  80. g_allocation_count = i;
  81. g_parser = XML_ParserCreate_MM(XCS("us-ascii"), &memsuite, NULL);
  82. if (g_parser != NULL)
  83. break;
  84. }
  85. if (i == 0)
  86. fail("Parser ignored failing allocator");
  87. else if (i == max_alloc_count)
  88. fail("Parser not created with max allocation count");
  89. }
  90. END_TEST
  91. /* Test that freeing a NULL parser doesn't cause an explosion.
  92. * (Not actually tested anywhere else)
  93. */
  94. START_TEST(test_misc_null_parser) {
  95. XML_ParserFree(NULL);
  96. }
  97. END_TEST
  98. #if defined(__has_feature)
  99. # if __has_feature(undefined_behavior_sanitizer)
  100. # define EXPAT_TESTS_UBSAN 1
  101. # else
  102. # define EXPAT_TESTS_UBSAN 0
  103. # endif
  104. #else
  105. # define EXPAT_TESTS_UBSAN 0
  106. #endif
  107. /* Test that XML_ErrorString rejects out-of-range codes */
  108. START_TEST(test_misc_error_string) {
  109. #if ! EXPAT_TESTS_UBSAN // because this would trigger UBSan
  110. union {
  111. enum XML_Error xml_error;
  112. int integer;
  113. } trickery;
  114. assert_true(sizeof(enum XML_Error) == sizeof(int)); // self-test
  115. trickery.integer = -1;
  116. if (XML_ErrorString(trickery.xml_error) != NULL)
  117. fail("Negative error code not rejected");
  118. trickery.integer = 100;
  119. if (XML_ErrorString(trickery.xml_error) != NULL)
  120. fail("Large error code not rejected");
  121. #endif
  122. }
  123. END_TEST
  124. /* Test the version information is consistent */
  125. /* Since we are working in XML_LChars (potentially 16-bits), we
  126. * can't use the standard C library functions for character
  127. * manipulation and have to roll our own.
  128. */
  129. static int
  130. parse_version(const XML_LChar *version_text,
  131. XML_Expat_Version *version_struct) {
  132. if (! version_text)
  133. return XML_FALSE;
  134. while (*version_text != 0x00) {
  135. if (*version_text >= ASCII_0 && *version_text <= ASCII_9)
  136. break;
  137. version_text++;
  138. }
  139. if (*version_text == 0x00)
  140. return XML_FALSE;
  141. /* version_struct->major = strtoul(version_text, 10, &version_text) */
  142. version_struct->major = 0;
  143. while (*version_text >= ASCII_0 && *version_text <= ASCII_9) {
  144. version_struct->major
  145. = 10 * version_struct->major + (*version_text++ - ASCII_0);
  146. }
  147. if (*version_text++ != ASCII_PERIOD)
  148. return XML_FALSE;
  149. /* Now for the minor version number */
  150. version_struct->minor = 0;
  151. while (*version_text >= ASCII_0 && *version_text <= ASCII_9) {
  152. version_struct->minor
  153. = 10 * version_struct->minor + (*version_text++ - ASCII_0);
  154. }
  155. if (*version_text++ != ASCII_PERIOD)
  156. return XML_FALSE;
  157. /* Finally the micro version number */
  158. version_struct->micro = 0;
  159. while (*version_text >= ASCII_0 && *version_text <= ASCII_9) {
  160. version_struct->micro
  161. = 10 * version_struct->micro + (*version_text++ - ASCII_0);
  162. }
  163. if (*version_text != 0x00)
  164. return XML_FALSE;
  165. return XML_TRUE;
  166. }
  167. static int
  168. versions_equal(const XML_Expat_Version *first,
  169. const XML_Expat_Version *second) {
  170. return (first->major == second->major && first->minor == second->minor
  171. && first->micro == second->micro);
  172. }
  173. START_TEST(test_misc_version) {
  174. XML_Expat_Version read_version = XML_ExpatVersionInfo();
  175. /* Silence compiler warning with the following assignment */
  176. XML_Expat_Version parsed_version = {0, 0, 0};
  177. const XML_LChar *version_text = XML_ExpatVersion();
  178. if (version_text == NULL)
  179. fail("Could not obtain version text");
  180. assert(version_text != NULL);
  181. if (! parse_version(version_text, &parsed_version))
  182. fail("Unable to parse version text");
  183. if (! versions_equal(&read_version, &parsed_version))
  184. fail("Version mismatch");
  185. if (xcstrcmp(version_text, XCS("expat_2.7.1"))) /* needs bump on releases */
  186. fail("XML_*_VERSION in expat.h out of sync?\n");
  187. }
  188. END_TEST
  189. /* Test feature information */
  190. START_TEST(test_misc_features) {
  191. const XML_Feature *features = XML_GetFeatureList();
  192. /* Prevent problems with double-freeing parsers */
  193. g_parser = NULL;
  194. if (features == NULL) {
  195. fail("Failed to get feature information");
  196. } else {
  197. /* Loop through the features checking what we can */
  198. while (features->feature != XML_FEATURE_END) {
  199. switch (features->feature) {
  200. case XML_FEATURE_SIZEOF_XML_CHAR:
  201. if (features->value != sizeof(XML_Char))
  202. fail("Incorrect size of XML_Char");
  203. break;
  204. case XML_FEATURE_SIZEOF_XML_LCHAR:
  205. if (features->value != sizeof(XML_LChar))
  206. fail("Incorrect size of XML_LChar");
  207. break;
  208. default:
  209. break;
  210. }
  211. features++;
  212. }
  213. }
  214. }
  215. END_TEST
  216. /* Regression test for GitHub Issue #17: memory leak parsing attribute
  217. * values with mixed bound and unbound namespaces.
  218. */
  219. START_TEST(test_misc_attribute_leak) {
  220. const char *text = "<D xmlns:L=\"D\" l:a='' L:a=''/>";
  221. XML_Memory_Handling_Suite memsuite
  222. = {tracking_malloc, tracking_realloc, tracking_free};
  223. g_parser = XML_ParserCreate_MM(XCS("UTF-8"), &memsuite, XCS("\n"));
  224. expect_failure(text, XML_ERROR_UNBOUND_PREFIX, "Unbound prefixes not found");
  225. XML_ParserFree(g_parser);
  226. /* Prevent the teardown trying to double free */
  227. g_parser = NULL;
  228. if (! tracking_report())
  229. fail("Memory leak found");
  230. }
  231. END_TEST
  232. /* Test parser created for UTF-16LE is successful */
  233. START_TEST(test_misc_utf16le) {
  234. const char text[] =
  235. /* <?xml version='1.0'?><q>Hi</q> */
  236. "<\0?\0x\0m\0l\0 \0"
  237. "v\0e\0r\0s\0i\0o\0n\0=\0'\0\x31\0.\0\x30\0'\0?\0>\0"
  238. "<\0q\0>\0H\0i\0<\0/\0q\0>\0";
  239. const XML_Char *expected = XCS("Hi");
  240. CharData storage;
  241. g_parser = XML_ParserCreate(XCS("UTF-16LE"));
  242. if (g_parser == NULL)
  243. fail("Parser not created");
  244. CharData_Init(&storage);
  245. XML_SetUserData(g_parser, &storage);
  246. XML_SetCharacterDataHandler(g_parser, accumulate_characters);
  247. if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)sizeof(text) - 1, XML_TRUE)
  248. == XML_STATUS_ERROR)
  249. xml_failure(g_parser);
  250. CharData_CheckXMLChars(&storage, expected);
  251. }
  252. END_TEST
  253. START_TEST(test_misc_stop_during_end_handler_issue_240_1) {
  254. XML_Parser parser;
  255. DataIssue240 *mydata;
  256. enum XML_Status result;
  257. const char *const doc1 = "<doc><e1/><e><foo/></e></doc>";
  258. parser = XML_ParserCreate(NULL);
  259. XML_SetElementHandler(parser, start_element_issue_240, end_element_issue_240);
  260. mydata = (DataIssue240 *)malloc(sizeof(DataIssue240));
  261. assert_true(mydata != NULL);
  262. mydata->parser = parser;
  263. mydata->deep = 0;
  264. XML_SetUserData(parser, mydata);
  265. result = _XML_Parse_SINGLE_BYTES(parser, doc1, (int)strlen(doc1), 1);
  266. XML_ParserFree(parser);
  267. free(mydata);
  268. if (result != XML_STATUS_ERROR)
  269. fail("Stopping the parser did not work as expected");
  270. }
  271. END_TEST
  272. START_TEST(test_misc_stop_during_end_handler_issue_240_2) {
  273. XML_Parser parser;
  274. DataIssue240 *mydata;
  275. enum XML_Status result;
  276. const char *const doc2 = "<doc><elem/></doc>";
  277. parser = XML_ParserCreate(NULL);
  278. XML_SetElementHandler(parser, start_element_issue_240, end_element_issue_240);
  279. mydata = (DataIssue240 *)malloc(sizeof(DataIssue240));
  280. assert_true(mydata != NULL);
  281. mydata->parser = parser;
  282. mydata->deep = 0;
  283. XML_SetUserData(parser, mydata);
  284. result = _XML_Parse_SINGLE_BYTES(parser, doc2, (int)strlen(doc2), 1);
  285. XML_ParserFree(parser);
  286. free(mydata);
  287. if (result != XML_STATUS_ERROR)
  288. fail("Stopping the parser did not work as expected");
  289. }
  290. END_TEST
  291. START_TEST(test_misc_deny_internal_entity_closing_doctype_issue_317) {
  292. const char *const inputOne
  293. = "<!DOCTYPE d [\n"
  294. "<!ENTITY % element_d '<!ELEMENT d (#PCDATA)*>'>\n"
  295. "%element_d;\n"
  296. "<!ENTITY % e ']><d/>'>\n"
  297. "\n"
  298. "%e;";
  299. const char *const inputTwo
  300. = "<!DOCTYPE d [\n"
  301. "<!ENTITY % element_d '<!ELEMENT d (#PCDATA)*>'>\n"
  302. "%element_d;\n"
  303. "<!ENTITY % e1 ']><d/>'><!ENTITY % e2 '&#37;e1;'>\n"
  304. "\n"
  305. "%e2;";
  306. const char *const inputThree
  307. = "<!DOCTYPE d [\n"
  308. "<!ENTITY % element_d '<!ELEMENT d (#PCDATA)*>'>\n"
  309. "%element_d;\n"
  310. "<!ENTITY % e ']><d'>\n"
  311. "\n"
  312. "%e;/>";
  313. const char *const inputIssue317
  314. = "<!DOCTYPE doc [\n"
  315. "<!ENTITY % element_doc '<!ELEMENT doc (#PCDATA)*>'>\n"
  316. "%element_doc;\n"
  317. "<!ENTITY % foo ']>\n"
  318. "<doc>Hell<oc (#PCDATA)*>'>\n"
  319. "%foo;\n"
  320. "]>\n"
  321. "<doc>Hello, world</dVc>";
  322. const char *const inputs[] = {inputOne, inputTwo, inputThree, inputIssue317};
  323. const XML_Bool suspendOrNot[] = {XML_FALSE, XML_TRUE};
  324. size_t inputIndex = 0;
  325. for (; inputIndex < sizeof(inputs) / sizeof(inputs[0]); inputIndex++) {
  326. for (size_t suspendOrNotIndex = 0;
  327. suspendOrNotIndex < sizeof(suspendOrNot) / sizeof(suspendOrNot[0]);
  328. suspendOrNotIndex++) {
  329. const char *const input = inputs[inputIndex];
  330. const XML_Bool suspend = suspendOrNot[suspendOrNotIndex];
  331. if (suspend && (g_chunkSize > 0)) {
  332. // We cannot use _XML_Parse_SINGLE_BYTES below due to suspension, and
  333. // so chunk sizes >0 would only repeat the very same test
  334. // due to use of plain XML_Parse; we are saving upon that runtime:
  335. return;
  336. }
  337. set_subtest("[input=%d suspend=%s] %s", (int)inputIndex,
  338. suspend ? "true" : "false", input);
  339. XML_Parser parser;
  340. enum XML_Status parseResult;
  341. int setParamEntityResult;
  342. XML_Size lineNumber;
  343. XML_Size columnNumber;
  344. parser = XML_ParserCreate(NULL);
  345. setParamEntityResult
  346. = XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
  347. if (setParamEntityResult != 1)
  348. fail("Failed to set XML_PARAM_ENTITY_PARSING_ALWAYS.");
  349. if (suspend) {
  350. XML_SetUserData(parser, parser);
  351. XML_SetElementDeclHandler(parser, suspend_after_element_declaration);
  352. }
  353. if (suspend) {
  354. // can't use SINGLE_BYTES here, because it'll return early on
  355. // suspension, and we won't know exactly how much input we actually
  356. // managed to give Expat.
  357. parseResult = XML_Parse(parser, input, (int)strlen(input), 0);
  358. while (parseResult == XML_STATUS_SUSPENDED) {
  359. parseResult = XML_ResumeParser(parser);
  360. }
  361. if (parseResult != XML_STATUS_ERROR) {
  362. // can't use SINGLE_BYTES here, because it'll return early on
  363. // suspension, and we won't know exactly how much input we actually
  364. // managed to give Expat.
  365. parseResult = XML_Parse(parser, "", 0, 1);
  366. }
  367. while (parseResult == XML_STATUS_SUSPENDED) {
  368. parseResult = XML_ResumeParser(parser);
  369. }
  370. } else {
  371. parseResult
  372. = _XML_Parse_SINGLE_BYTES(parser, input, (int)strlen(input), 0);
  373. if (parseResult != XML_STATUS_ERROR) {
  374. parseResult = _XML_Parse_SINGLE_BYTES(parser, "", 0, 1);
  375. }
  376. }
  377. if (parseResult != XML_STATUS_ERROR) {
  378. fail("Parsing was expected to fail but succeeded.");
  379. }
  380. if (XML_GetErrorCode(parser) != XML_ERROR_INVALID_TOKEN)
  381. fail("Error code does not match XML_ERROR_INVALID_TOKEN");
  382. lineNumber = XML_GetCurrentLineNumber(parser);
  383. if (lineNumber != 6)
  384. fail("XML_GetCurrentLineNumber does not work as expected.");
  385. columnNumber = XML_GetCurrentColumnNumber(parser);
  386. if (columnNumber != 0)
  387. fail("XML_GetCurrentColumnNumber does not work as expected.");
  388. XML_ParserFree(parser);
  389. }
  390. }
  391. }
  392. END_TEST
  393. START_TEST(test_misc_tag_mismatch_reset_leak) {
  394. #ifdef XML_NS
  395. const char *const text = "<open xmlns='https://namespace1.test'></close>";
  396. XML_Parser parser = XML_ParserCreateNS(NULL, XCS('\n'));
  397. if (_XML_Parse_SINGLE_BYTES(parser, text, (int)strlen(text), XML_TRUE)
  398. != XML_STATUS_ERROR)
  399. fail("Call to parse was expected to fail");
  400. if (XML_GetErrorCode(parser) != XML_ERROR_TAG_MISMATCH)
  401. fail("Call to parse was expected to fail from a closing tag mismatch");
  402. XML_ParserReset(parser, NULL);
  403. if (_XML_Parse_SINGLE_BYTES(parser, text, (int)strlen(text), XML_TRUE)
  404. != XML_STATUS_ERROR)
  405. fail("Call to parse was expected to fail");
  406. if (XML_GetErrorCode(parser) != XML_ERROR_TAG_MISMATCH)
  407. fail("Call to parse was expected to fail from a closing tag mismatch");
  408. XML_ParserFree(parser);
  409. #endif
  410. }
  411. END_TEST
  412. START_TEST(test_misc_create_external_entity_parser_with_null_context) {
  413. // With XML_DTD undefined, the only supported case of external entities
  414. // is pattern "<!ENTITY entity123 SYSTEM 'filename123'>". A NULL context
  415. // was causing a segfault through a null pointer dereference in function
  416. // setContext, previously.
  417. XML_Parser parser = XML_ParserCreate(NULL);
  418. XML_Parser ext_parser = XML_ExternalEntityParserCreate(parser, NULL, NULL);
  419. #ifdef XML_DTD
  420. assert_true(ext_parser != NULL);
  421. XML_ParserFree(ext_parser);
  422. #else
  423. assert_true(ext_parser == NULL);
  424. #endif /* XML_DTD */
  425. XML_ParserFree(parser);
  426. }
  427. END_TEST
  428. START_TEST(test_misc_general_entities_support) {
  429. const char *const doc
  430. = "<!DOCTYPE r [\n"
  431. "<!ENTITY e1 'v1'>\n"
  432. "<!ENTITY e2 SYSTEM 'v2'>\n"
  433. "]>\n"
  434. "<r a1='[&e1;]'>[&e1;][&e2;][&amp;&apos;&gt;&lt;&quot;]</r>";
  435. CharData storage;
  436. CharData_Init(&storage);
  437. XML_Parser parser = XML_ParserCreate(NULL);
  438. XML_SetUserData(parser, &storage);
  439. XML_SetStartElementHandler(parser, accumulate_start_element);
  440. XML_SetExternalEntityRefHandler(parser,
  441. external_entity_failer__if_not_xml_ge);
  442. XML_SetEntityDeclHandler(parser, accumulate_entity_decl);
  443. XML_SetCharacterDataHandler(parser, accumulate_characters);
  444. if (_XML_Parse_SINGLE_BYTES(parser, doc, (int)strlen(doc), XML_TRUE)
  445. != XML_STATUS_OK) {
  446. xml_failure(parser);
  447. }
  448. XML_ParserFree(parser);
  449. CharData_CheckXMLChars(&storage,
  450. /* clang-format off */
  451. #if XML_GE == 1
  452. XCS("e1=v1\n")
  453. XCS("e2=(null)\n")
  454. XCS("(r(a1=[v1]))\n")
  455. XCS("[v1][][&'><\"]")
  456. #else
  457. XCS("e1=&amp;e1;\n")
  458. XCS("e2=(null)\n")
  459. XCS("(r(a1=[&e1;]))\n")
  460. XCS("[&e1;][&e2;][&'><\"]")
  461. #endif
  462. );
  463. /* clang-format on */
  464. }
  465. END_TEST
  466. static void XMLCALL
  467. resumable_stopping_character_handler(void *userData, const XML_Char *s,
  468. int len) {
  469. UNUSED_P(s);
  470. UNUSED_P(len);
  471. XML_Parser parser = (XML_Parser)userData;
  472. XML_StopParser(parser, XML_TRUE);
  473. }
  474. // NOTE: This test needs active LeakSanitizer to be of actual use
  475. START_TEST(test_misc_char_handler_stop_without_leak) {
  476. const char *const data
  477. = "<!DOCTYPE t1[<!ENTITY e1 'angle<'><!ENTITY e2 '&e1;'>]><t1>&e2;";
  478. XML_Parser parser = XML_ParserCreate(NULL);
  479. assert_true(parser != NULL);
  480. XML_SetUserData(parser, parser);
  481. XML_SetCharacterDataHandler(parser, resumable_stopping_character_handler);
  482. _XML_Parse_SINGLE_BYTES(parser, data, (int)strlen(data), XML_FALSE);
  483. XML_ParserFree(parser);
  484. }
  485. END_TEST
  486. START_TEST(test_misc_resumeparser_not_crashing) {
  487. XML_Parser parser = XML_ParserCreate(NULL);
  488. XML_GetBuffer(parser, 1);
  489. XML_StopParser(parser, /*resumable=*/XML_TRUE);
  490. XML_ResumeParser(parser); // could crash here, previously
  491. XML_ParserFree(parser);
  492. }
  493. END_TEST
  494. START_TEST(test_misc_stopparser_rejects_unstarted_parser) {
  495. const XML_Bool cases[] = {XML_TRUE, XML_FALSE};
  496. for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
  497. const XML_Bool resumable = cases[i];
  498. XML_Parser parser = XML_ParserCreate(NULL);
  499. assert_true(XML_GetErrorCode(parser) == XML_ERROR_NONE);
  500. assert_true(XML_StopParser(parser, resumable) == XML_STATUS_ERROR);
  501. assert_true(XML_GetErrorCode(parser) == XML_ERROR_NOT_STARTED);
  502. XML_ParserFree(parser);
  503. }
  504. }
  505. END_TEST
  506. /* Adaptation of accumulate_characters that takes ExtHdlrData input to work with
  507. * test_renter_loop_finite_content below */
  508. void XMLCALL
  509. accumulate_characters_ext_handler(void *userData, const XML_Char *s, int len) {
  510. ExtHdlrData *const test_data = (ExtHdlrData *)userData;
  511. CharData_AppendXMLChars(test_data->storage, s, len);
  512. }
  513. /* Test that internalEntityProcessor does not re-enter forever;
  514. * based on files tests/xmlconf/xmltest/valid/ext-sa/012.{xml,ent} */
  515. START_TEST(test_renter_loop_finite_content) {
  516. CharData storage;
  517. CharData_Init(&storage);
  518. const char *const text = "<!DOCTYPE doc [\n"
  519. "<!ENTITY e1 '&e2;'>\n"
  520. "<!ENTITY e2 '&e3;'>\n"
  521. "<!ENTITY e3 SYSTEM '012.ent'>\n"
  522. "<!ENTITY e4 '&e5;'>\n"
  523. "<!ENTITY e5 '(e5)'>\n"
  524. "<!ELEMENT doc (#PCDATA)>\n"
  525. "]>\n"
  526. "<doc>&e1;</doc>\n";
  527. ExtHdlrData test_data = {"&e4;\n", external_entity_null_loader, &storage};
  528. const XML_Char *const expected = XCS("(e5)\n");
  529. XML_Parser parser = XML_ParserCreate(NULL);
  530. assert_true(parser != NULL);
  531. XML_SetUserData(parser, &test_data);
  532. XML_SetExternalEntityRefHandler(parser, external_entity_oneshot_loader);
  533. XML_SetCharacterDataHandler(parser, accumulate_characters_ext_handler);
  534. if (_XML_Parse_SINGLE_BYTES(parser, text, (int)strlen(text), XML_TRUE)
  535. == XML_STATUS_ERROR)
  536. xml_failure(parser);
  537. CharData_CheckXMLChars(&storage, expected);
  538. XML_ParserFree(parser);
  539. }
  540. END_TEST
  541. // Inspired by function XML_OriginalString of Perl's XML::Parser
  542. static char *
  543. dup_original_string(XML_Parser parser) {
  544. const int byte_count = XML_GetCurrentByteCount(parser);
  545. assert_true(byte_count >= 0);
  546. int offset = -1;
  547. int size = -1;
  548. const char *const context = XML_GetInputContext(parser, &offset, &size);
  549. #if XML_CONTEXT_BYTES > 0
  550. assert_true(context != NULL);
  551. assert_true(offset >= 0);
  552. assert_true(size >= 0);
  553. return portable_strndup(context + offset, byte_count);
  554. #else
  555. assert_true(context == NULL);
  556. return NULL;
  557. #endif
  558. }
  559. static void
  560. on_characters_issue_980(void *userData, const XML_Char *s, int len) {
  561. (void)s;
  562. (void)len;
  563. XML_Parser parser = (XML_Parser)userData;
  564. char *const original_string = dup_original_string(parser);
  565. #if XML_CONTEXT_BYTES > 0
  566. assert_true(original_string != NULL);
  567. assert_true(strcmp(original_string, "&draft.day;") == 0);
  568. free(original_string);
  569. #else
  570. assert_true(original_string == NULL);
  571. #endif
  572. }
  573. START_TEST(test_misc_expected_event_ptr_issue_980) {
  574. // NOTE: This is a tiny subset of sample "REC-xml-19980210.xml"
  575. // from Perl's XML::Parser
  576. const char *const doc = "<!DOCTYPE day [\n"
  577. " <!ENTITY draft.day '10'>\n"
  578. "]>\n"
  579. "<day>&draft.day;</day>\n";
  580. XML_Parser parser = XML_ParserCreate(NULL);
  581. XML_SetUserData(parser, parser);
  582. XML_SetCharacterDataHandler(parser, on_characters_issue_980);
  583. assert_true(_XML_Parse_SINGLE_BYTES(parser, doc, (int)strlen(doc),
  584. /*isFinal=*/XML_TRUE)
  585. == XML_STATUS_OK);
  586. XML_ParserFree(parser);
  587. }
  588. END_TEST
  589. void
  590. make_miscellaneous_test_case(Suite *s) {
  591. TCase *tc_misc = tcase_create("miscellaneous tests");
  592. suite_add_tcase(s, tc_misc);
  593. tcase_add_checked_fixture(tc_misc, NULL, basic_teardown);
  594. tcase_add_test(tc_misc, test_misc_alloc_create_parser);
  595. tcase_add_test(tc_misc, test_misc_alloc_create_parser_with_encoding);
  596. tcase_add_test(tc_misc, test_misc_null_parser);
  597. tcase_add_test(tc_misc, test_misc_error_string);
  598. tcase_add_test(tc_misc, test_misc_version);
  599. tcase_add_test(tc_misc, test_misc_features);
  600. tcase_add_test(tc_misc, test_misc_attribute_leak);
  601. tcase_add_test(tc_misc, test_misc_utf16le);
  602. tcase_add_test(tc_misc, test_misc_stop_during_end_handler_issue_240_1);
  603. tcase_add_test(tc_misc, test_misc_stop_during_end_handler_issue_240_2);
  604. tcase_add_test__ifdef_xml_dtd(
  605. tc_misc, test_misc_deny_internal_entity_closing_doctype_issue_317);
  606. tcase_add_test(tc_misc, test_misc_tag_mismatch_reset_leak);
  607. tcase_add_test(tc_misc,
  608. test_misc_create_external_entity_parser_with_null_context);
  609. tcase_add_test(tc_misc, test_misc_general_entities_support);
  610. tcase_add_test(tc_misc, test_misc_char_handler_stop_without_leak);
  611. tcase_add_test(tc_misc, test_misc_resumeparser_not_crashing);
  612. tcase_add_test(tc_misc, test_misc_stopparser_rejects_unstarted_parser);
  613. tcase_add_test__if_xml_ge(tc_misc, test_renter_loop_finite_content);
  614. tcase_add_test(tc_misc, test_misc_expected_event_ptr_issue_980);
  615. }