Skip to content

Fix crash during block math parsing#142

Merged
LiYanan2004 merged 1 commit intomainfrom
fix/math-block-paring-crash
Feb 28, 2026
Merged

Fix crash during block math parsing#142
LiYanan2004 merged 1 commit intomainfrom
fix/math-block-paring-crash

Conversation

@LiYanan2004
Copy link
Owner

Closes #141

Copilot AI review requested due to automatic review settings February 28, 2026 10:04
@LiYanan2004 LiYanan2004 merged commit b93e915 into main Feb 28, 2026
9 checks passed
@LiYanan2004 LiYanan2004 deleted the fix/math-block-paring-crash branch February 28, 2026 10:07
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a crash in MathFirstMarkdownViewRenderer when rendering Markdown that contains both fenced code blocks and block (display) math by avoiding range invalidation during in-place string mutation.

Changes:

  • Iterate parsable segments in reverse order so later replacements don’t invalidate earlier segment ranges.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +21 to 23
for range in extractor.parsableRanges(in: rawText).reversed() {
let segment = rawText[range]
let segmentParser = MathParser(text: segment)
Copy link

Copilot AI Feb 28, 2026

Choose a reason for hiding this comment

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

Even with iterating ranges in reverse, this loop still reuses precomputed Range<String.Index> values after mutating rawText (via replaceSubrange). Swift String mutations can invalidate existing indices/ranges, which can still lead to crashes in edge cases. Consider capturing ranges as integer offsets (e.g., UTF-16 offsets) and recreating fresh String.Index values on the current rawText before slicing/replacing, or perform replacements on a copied String segment and then splice the segment back into rawText.

Copilot uses AI. Check for mistakes.
var extractor = ParsingRangesExtractor()
extractor.visit(content.parse(options: ParseOptions().union(.parseBlockDirectives)))
for range in extractor.parsableRanges(in: rawText) {
for range in extractor.parsableRanges(in: rawText).reversed() {
Copy link

Copilot AI Feb 28, 2026

Choose a reason for hiding this comment

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

This change fixes a crash scenario (math blocks + code blocks). Please add a regression test that renders markdown containing both a fenced code block and a display-math block to ensure MathFirstMarkdownViewRenderer no longer crashes when processing multiple parsable segments.

Copilot uses AI. Check for mistakes.
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.

当渲染的 Markdown 中同时包含代码块和公式块的时候,MathFirstMarkdownViewRenderer 中会崩溃

2 participants