Repository navigation
Fix crash when emoji precedes a later block - #25
Open
feng2919013386-oss wants to merge 1 commit into
Open
feng2919013386-oss wants to merge 1 commit into
feng2919013386-oss wants to merge 1 commit into
Conversation
applyContextualSpacing indexed the attributed string's UTF-16 length (result.length) into a Swift String by Character offset. Any supplementary-plane character (emoji) rendered before the block makes UTF-16 length exceed the Character count, and String.index(_:offsetBy:) traps with 'String index is out of bounds' — a hard crash on launch for any emoji-containing document with two or more blocks. Index by UTF-16 offset (String.Index(utf16Offset:in:)) and measure the paragraph length in the UTF-16 view, since NSRange lengths are UTF-16. Adds regression tests covering emoji before later blocks and a longer emoji-heavy document (headings, lists, blockquotes, code, tables).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
MarkdownRenderer.applyContextualSpacingcrashed withString index is out of bounds(EXC_BREAKPOINT/SIGTRAP) on documents containing supplementary-plane characters (emoji, ZWJ family emoji, flags, etc.) before a later block.Root cause:
startPositionisresult.length— a count of UTF-16 code units — but the code advanced a SwiftString.Indexwithtext.index(text.startIndex, offsetBy: startPosition), which counts Characters (grapheme clusters). For any multi-unit character rendered before the block, the offset exceeds the Character count and Swift traps — an immediate crash on open for any such document.Reproduced with
open mrkd.app fulla.md, a file whose first paragraph is 150 × 👨👩👧👦 (1050 UTF-16 units, 150 Characters), followed by a second paragraph: the pre-fix build crashes on launch, the fixed build renders normally.Fix
String.Index(utf16Offset:startPosition, in: text)text.utf16.distance(...)), since it is used as anNSRangelength (also UTF-16)Tests
Added two regression tests in
MarkdownRendererTests:testRenderDoesNotCrashWhenEmojiPrecedesLaterBlock— the minimal crash case, also asserts contextual spacing is still appliedtestRenderSurvivesEmojiInsideLongerDocument— emoji in headings, lists, blockquotes, code fences and tables