From 4978d285d205d1238c823876134c3e486a3c2fe5 Mon Sep 17 00:00:00 2001 From: Snild Dolkow Date: Thu, 31 Aug 2023 14:35:48 +0200 Subject: [PATCH] tests: Look for single-char match in test_abort_epilog ...instead of a full-string match. These tests were depending on getting handler callbacks with exactly one character of data at a time. For example, if test_abort_epilog got "\n\r\n" in one callback, it would fail to match on the '\r', and would not abort parsing as expected. By searching the callback arg for the magic character rather than expecting a full match, the test no longer depends on exact callback timing. `userData` is never NULL in these tests, so that check was left out of the new version. --- expat/tests/basic_tests.c | 12 ++++++------ expat/tests/handlers.c | 13 ++++++++++--- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/expat/tests/basic_tests.c b/expat/tests/basic_tests.c index 634288ad..8b47e5fe 100644 --- a/expat/tests/basic_tests.c +++ b/expat/tests/basic_tests.c @@ -3474,10 +3474,10 @@ END_TEST /* Test aborting the parse in an epilog works */ START_TEST(test_abort_epilog) { const char *text = "\n\r\n"; - XML_Char match[] = XCS("\r"); + XML_Char trigger_char = XCS('\r'); XML_SetDefaultHandler(g_parser, selective_aborting_default_handler); - XML_SetUserData(g_parser, match); + XML_SetUserData(g_parser, &trigger_char); g_resumable = XML_FALSE; if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE) != XML_STATUS_ERROR) @@ -3490,10 +3490,10 @@ END_TEST /* Test a different code path for abort in the epilog */ START_TEST(test_abort_epilog_2) { const char *text = "\n"; - XML_Char match[] = XCS("\n"); + XML_Char trigger_char = XCS('\n'); XML_SetDefaultHandler(g_parser, selective_aborting_default_handler); - XML_SetUserData(g_parser, match); + XML_SetUserData(g_parser, &trigger_char); g_resumable = XML_FALSE; expect_failure(text, XML_ERROR_ABORTED, "Abort not triggered"); } @@ -3502,10 +3502,10 @@ END_TEST /* Test suspension from the epilog */ START_TEST(test_suspend_epilog) { const char *text = "\n"; - XML_Char match[] = XCS("\n"); + XML_Char trigger_char = XCS('\n'); XML_SetDefaultHandler(g_parser, selective_aborting_default_handler); - XML_SetUserData(g_parser, match); + XML_SetUserData(g_parser, &trigger_char); g_resumable = XML_TRUE; if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE) != XML_STATUS_SUSPENDED) diff --git a/expat/tests/handlers.c b/expat/tests/handlers.c index 847933b8..c347c3a2 100644 --- a/expat/tests/handlers.c +++ b/expat/tests/handlers.c @@ -1793,10 +1793,17 @@ data_check_comment_handler(void *userData, const XML_Char *data) { void XMLCALL selective_aborting_default_handler(void *userData, const XML_Char *s, int len) { - const XML_Char *match = (const XML_Char *)userData; + const XML_Char trigger_char = *(const XML_Char *)userData; - if (match == NULL - || (xcstrlen(match) == (unsigned)len && ! xcstrncmp(match, s, len))) { + int found = 0; + for (int i = 0; i < len; ++i) { + if (s[i] == trigger_char) { + found = 1; + break; + } + } + + if (found) { XML_StopParser(g_parser, g_resumable); XML_SetDefaultHandler(g_parser, NULL); }