From f9f1a3cbabf8fbebc4dc331c7a85e0392f2b982a Mon Sep 17 00:00:00 2001 From: mausimus <73635663+mausimus@users.noreply.github.com> Date: Wed, 19 Jun 2024 20:52:08 +0900 Subject: [PATCH] AGS: Parser fixes from upstream Compilation of three bug fixes to text parser I recently made in the AGS project, around alternatives syntax: 1. Stop parsing when reaching end during alternatives skipping (causing an out-of-bounds memory read) 2. Correctly skip over multi-word alternatives (incorrect parsing of alternative lists) 3. Use dedicated function to identify word boundaries (affecting dash and apostrophe containing alternatives) Upstream commits (release-3.6.1 branch): 1. 17f8ea2f0efadec7b3696d4ba51733f1cddc0772 (check for end of input not to go beyond) 2. e98315393a34629b8935fdee7bd725a8299f941c (fix multi-word alternative skipping) 3. 9b0ccbd04e36e757392b1fc744919c785310c57b (consistently check for word boundaries) --- engines/ags/engine/ac/parser.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/engines/ags/engine/ac/parser.cpp b/engines/ags/engine/ac/parser.cpp index 4884a962a78..bc54eaebe62 100644 --- a/engines/ags/engine/ac/parser.cpp +++ b/engines/ags/engine/ac/parser.cpp @@ -242,8 +242,13 @@ int parse_sentence(const char *src_text, int *numwords, short *wordarray, short const char *textStart = ++text; // begin with next char // find where the next word ends - while ((text[0] == ',') || (Common::isAlnum((unsigned char)text[0]) != 0)) - text++; + while ((text[0] == ',') || is_valid_word_char(text[0])) { + // shift beginning of potential multi-word each time we see a comma + if (text[0] == ',') + textStart = ++text; + else + text++; + } continueSearching = 0; @@ -251,8 +256,11 @@ int parse_sentence(const char *src_text, int *numwords, short *wordarray, short Common::strcpy_s(thisword, textStart); thisword[text - textStart] = 0; // forward past any multi-word alternatives - if (FindMatchingMultiWordWord(thisword, &text) >= 0) + if (FindMatchingMultiWordWord(thisword, &text) >= 0) { + if (text[0] == 0) + break; continueSearching = 1; + } } }