misc_tests.c 28 KB

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