Skip to content

fix(lexer): close the group after an escaped backslash - #23

Merged
d0rianb merged 1 commit into
d0rianb:masterfrom
trypsynth:escaped-backslash-before-brace
Nov 2, 2026
Merged

d0rianb merged 1 commit into
d0rianb:masterfrom
trypsynth:escaped-backslash-before-brace

Conversation

@trypsynth

Copy link
Copy Markdown
Contributor

Lexer::scan treats any {, }, \ or newline that follows a backslash as escaped. An escaped backslash (\\) is itself a complete escape, though, so in \\} the brace was taken as escaped as well. The group never closed, and every token after it was one bracket off.

Word writes exactly this inside field instructions, for example the bullets it stores as

{\field{\*\fldinst {\f6 SYMBOL 183 }{\f6 \\}{\f6 f "Symbol" }}{\fldrslt }}

so a reader skipping the field's instruction group never saw it end, and lost the rest of the document. I found it while running a few hundred real-world RTF files (shareware help sources from 1990s CD-ROMs) through Paperback, which uses this crate.

The fix tracks whether the previous backslash still escapes the current character, so the second backslash of \\ escapes nothing. scan_escaped_backslash_before_closing_bracket covers it: before the change it gives PlainText("\\}after") and one closing bracket too few.

The scanner treated any "{", "}", "\" or newline right after a backslash as escaped, so in "\}" the brace was taken as escaped too: the escaped backslash escaped the next character as well. The group never closed, and every token after it was off by one bracket. Word writes exactly this inside field instructions such as {\fldinst SYMBOL 183 \f "Symbol"}, and documents containing them lost everything that followed.

@d0rianb d0rianb left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, thanks

@d0rianb
d0rianb merged commit 6372276 into d0rianb:master Nov 2, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants