iOS: NSRangeException crash drawing truncated <Text> when the ellipsis boundary splits a composed character cluster (processTruncatedAttributedText)
維護者通常 1 天內回覆
評估
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 新手友好度
- 65/100
- Issue 類型
- 缺陷
- 描述清晰度
- 描述清楚
- 活躍度
- 冷清
- 技術堆疊
- ios, objective-c, react-native
- 領域
- mobile
研究方向
從 ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm 中的 processTruncatedAttributedText 開始,使用連結的 Snack 在 iOS 裝置或模擬器上重現當機。追蹤 issue 中描述的列舉、屬性編輯和 highlight pass,然後確認 composed-character width sweep 在繪製期間不再引發 NSRangeException。
由索引模型根據 Issue 內容生成。
描述
Description
Rendering a <Text numberOfLines={1}> whose content contains composed character clusters (stacked combining marks, e.g. Zalgo-style user display names) crashes the app on iOS with an uncatchable NSRangeException when the truncation ellipsis boundary falls inside one of those clusters:
NSRangeException: *** -[NSConcreteTextStorage attributesAtIndex:longestEffectiveRange:inRange:]: Range or index out of bounds
(also observed as NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds)
This is a production crash for us: any user whose friend/chat contact has such a display name gets a deterministic crash on every visit to the screen rendering it. Because the throw happens on the main thread inside Core Animation's draw pass, it is not catchable from JS and always aborts the app.
Root cause
The problem is in processTruncatedAttributedText in ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm (introduced for #37926, unchanged on main at time of filing):
drawAttributedString:paragraphAttributes:frame:drawHighlightPath:computesglyphRange, then callsprocessTruncatedAttributedText.- When
maximumNumberOfLines > 0and truncation is active, that method mutates theNSTextStorage(removeAttribute:/addAttribute:for foreground/background color on the truncated character range) insideenumerateLineFragmentsForGlyphRange:— mutating a text storage during its own layout manager's enumeration is unsupported by TextKit. - Each mutation triggers attribute fixing (
fixAttributesInRange), which snaps attribute runs to composed-character-sequence boundaries. When the truncated character range starts mid-cluster, the runs shift relative to the layout manager's already-computed glyph→character mapping. - The subsequent
drawGlyphsForGlyphRange:/ the highlight pass (characterRangeForGlyphRange:+enumerateAttribute:inRange:) query the storage with stale ranges →NSRangeException→ SIGABRT.
Suggested fix direction
Collect the truncated character ranges during enumerateLineFragmentsForGlyphRange: and apply the attribute edits after the enumeration (wrapped in beginEditing/endEditing); expand ranges with rangeOfComposedCharacterSequencesForRange: before mutating; and clamp with NSIntersectionRange against textStorage.length before enumerateAttribute:inRange: in the highlight pass.
Steps to reproduce
- Open the Snack on an iOS device or simulator (not web): https://snack.expo.dev/@normanwilde.equals/nsrangeexception-crash
- Scroll through the list.
- The app crashes with
NSRangeExceptiononce a row's width truncates the string mid-cluster.
The Snack renders this production-sourced display name at container widths 20→200pt in 0.5pt steps with numberOfLines={1}:
◍🥝۪〬.࠭⤿𝓚𝔀𝓜◍🥭۪〬
(22 code points / ~8 visible glyphs: combining marks — including U+20DD COMBINING ENCLOSING CIRCLE and Arabic/Meetei Mayek marks — stacked on symbol and emoji bases, plus Mathematical Script letters.)
The width sweep matters: the crash only fires at widths where the ellipsis cuts inside a composed cluster, so a single fixed width usually misses — which is why this is hard to hit in a trivial test yet deterministic in real layouts.
React Native Version
0.85.3
Affected Platforms
Runtime - iOS
Output of npx @react-native-community/cli info
System:
OS: macOS 26.5
CPU: (10) arm64 Apple M4
Memory: 215.69 MB / 16.00 GB
Shell:
version: "5.9"
path: /bin/zsh
Binaries:
Node:
version: 22.22.2
path: ~/.nvm/versions/node/v22.22.2/bin/node
Yarn:
version: 4.7.0
path: /opt/homebrew/bin/yarn
npm:
version: 10.9.7
path: ~/.nvm/versions/node/v22.22.2/bin/npm
Watchman:
version: 2024.07.29.00
path: /opt/homebrew/bin/watchman
Managers:
CocoaPods:
version: 1.16.2
path: ~/.rbenv/shims/pod
SDKs:
iOS SDK:
Platforms:
- DriverKit 25.5
- iOS 26.5
- macOS 26.5
- tvOS 26.5
- visionOS 26.5
- watchOS 26.5
IDEs:
Xcode:
version: 26.6/17F113
path: /usr/bin/xcodebuild
Languages:
Ruby:
version: 3.3.4
npmPackages:
react:
installed: 19.2.3
wanted: 19.2.3
react-native:
installed: 0.85.3
wanted: 0.85.3
Stacktrace or Logs
NSRangeException: *** -[NSConcreteTextStorage attributesAtIndex:longestEffectiveRange:inRange:]: Range or index out of bounds
0 CoreFoundation __exceptionPreprocess
1 libobjc.A.dylib objc_exception_throw
2 UIFoundation (NSLayoutManager internals)
3 UIFoundation (NSLayoutManager internals)
4 React -[RCTTextLayoutManager drawAttributedString:paragraphAttributes:frame:drawHighlightPath:] + 616 (RCTTextLayoutManager.mm:98)
5 React -[RCTParagraphTextView drawRect:] + 548 (RCTParagraphComponentView.mm:409)
6 UIKitCore (CALayerDelegate drawLayer:inContext:)
7 UIKitCore UIGraphicsPushContext
9 QuartzCore CABackingStoreUpdate_
12 QuartzCore CA::Layer::layout_and_display_if_needed
14 QuartzCore CA::Transaction::commit()
... (main run loop / UIApplicationMain)
MANDATORY Reproducer
https://snack.expo.dev/@normanwilde.equals/nsrangeexception-crash
Screenshots and Videos
No response
- 主要語言
- C++
- 星號
- 127k
- 分支
- 25.3k
- PR 合併指標
- 30 天內沒有已合併 PR
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
react/react-native 的其他 Issue
-
Needs: Author Feedback Needs: Repro
難度 2/5 1-3 小時 新手友好度 76/100
react/react-native#58952 · 1 則留言 ·
維護者通常 1 天內回覆
-
Needs: Attention Needs: Repro
難度 2/5 1-3 小時 新手友好度 78/100
react/react-native#58904 · 2 則留言 ·
維護者通常 1 天內回覆
-
Needs: Author Feedback Needs: Repro
難度 2/5 1-3 小時 新手友好度 75/100
react/react-native#58659 · 2 則留言 ·
維護者通常 1 天內回覆
-
Needs: Author Feedback Needs: Repro
難度 1/5 1 小時以內 新手友好度 92/100
react/react-native#58621 · 1 則留言 ·
維護者通常 1 天內回覆
-
Add TypeScript declaration for the performance global可能已有人在做 @Cedric921 於 6 天前認領。 未關閉Needs: Attention Needs: Repro
難度 2/5 1-3 小時 新手友好度 85/100
react/react-native#58610 · 4 則留言 ·
維護者通常 1 天內回覆
查看 react/react-native 的全部 Issue
相似的 Issue
-
Incorrect Link in README.md可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉
難度 1/5 1 小時以內 新手友好度 95/100
flameshot-org/flameshot#4996 ·
維護者通常 2 天內回覆
-
難度 2/5 1-3 小時 新手友好度 64/100
utopia-rise/godot-jvm#1004 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 70/100
維護者通常 3 天內回覆
-
難度 2/5 1-3 小時 新手友好度 70/100
維護者通常 1 天內回覆
-
chore(build): TxCoordinator.cpp uses the deprecated shared_ptr atomic free functions可能已有人在做 @w5jwp 今天認領。 未關閉
難度 1/5 1 小時以內 新手友好度 84/100
aethersdr/AetherSDR#6368 · 1 則留言 ·
維護者通常 1 天內回覆