Automattic / Automattic/drawText

`drawText` crashes when dealing with Arabic text

Open
#1 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
1
Forks
2
PR merge metrics
No merged PRs in 30d

Description

When running `drawText` as part of my work on promo screenshots on WPAndroid, I encountered repetitive Out of Bounds crashes when using a `html=` file containing Arabic characters:

```
[19:07:08]: $ drawText html\=/Users/olivierhalligon/Documents/Dev/apps/WordPress-Android/fastlane/metadata/android/ar/screenshot_2.txt maxWidth\=750 maxHeight\=350 output\=/var/folders/fn/g0dpb1lx345g5lg0s65wlp800000gn/T/20221122-11826-1gku8o fontSize\=80px stylesheet\=./fastlane/screenshots/assets/style.css alignment\=center
[19:07:08]: ▸ 2022-11-22 19:07:08.660 drawText[12039:173088] *** Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds'
[19:07:08]: ▸ *** First throw call stack:
[19:07:08]: ▸ (
[19:07:08]: ▸ 0 CoreFoundation 0x00000001888c4418 __exceptionPreprocess + 176
[19:07:08]: ▸ 1 libobjc.A.dylib 0x000000018840eea8 objc_exception_throw + 60
[19:07:08]: ▸ 2 Foundation 0x000000018975a5b4 blockForLocation + 0
[19:07:08]: ▸ 3 Foundation 0x0000000189770bc4 -[NSMutableAttributedString addAttributes:range:] + 96
[19:07:08]: ▸ 4 UIFoundation 0x000000018c989840 __NSConcreteTextStorageLockedForwarding + 72
[19:07:08]: ▸ 5 UIFoundation 0x000000018c9e71f4 -[NSConcreteTextStorage addAttributes:range:] + 100
[19:07:08]: ▸ 6 drawText 0x0000000104155e18 $s8drawText04CoreB5StackV20applyParagraphStylesyyF + 328
[19:07:08]: ▸ 7 drawText 0x0000000104156098 $s8drawText04CoreB5StackV4html4size9alignmentAC10Foundation4DataV_So6CGSizeVSo15NSTextAlignmentVtKcfCTf4gnnd_n + 512
[19:07:08]: ▸ 8 drawText 0x0000000104158be8 $s8drawText0B5ImageC0A06inSizeSo10CGImageRefaSgSo6CGSizeV_tKF + 420
[19:07:08]: ▸ 9 drawText 0x0000000104154294 main + 1612
[19:07:08]: ▸ 10 dyld 0x000000018843fe50 start + 2544
[19:07:08]: ▸ )
[19:07:08]: ▸ libc++abi: terminating with uncaught exception of type NSException
[19:07:08]: ▸ 2022-11-22 19:07:08.813 drawText[12048:173129] *** Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds'
[19:07:08]: ▸ *** First throw call stack:
[19:07:08]: ▸ (
[19:07:08]: ▸ 0 CoreFoundation 0x00000001888c4418 __exceptionPreprocess + 176
[19:07:08]: ▸ 1 libobjc.A.dylib 0x000000018840eea8 objc_exception_throw + 60
[19:07:08]: ▸ 2 Foundation 0x000000018975a5b4 blockForLocation + 0
[19:07:08]: ▸ 3 Foundation 0x0000000189770bc4 -[NSMutableAttributedString addAttributes:range:] + 96
[19:07:08]: ▸ 4 UIFoundation 0x000000018c989840 __NSConcreteTextStorageLockedForwarding + 72
[19:07:08]: ▸ 5 UIFoundation 0x000000018c9e71f4 -[NSConcreteTextStorage addAttributes:range:] + 100
[19:07:08]: ▸ 6 drawText 0x0000000104955e18 $s8drawText04CoreB5StackV20applyParagraphStylesyyF + 328
[19:07:08]: ▸ 7 drawText 0x0000000104956098 $s8drawText04CoreB5StackV4html4size9alignmentAC10Foundation4DataV_So6CGSizeVSo15NSTextAlignmentVtKcfCTf4gnnd_n + 512
[19:07:08]: ▸ 8 drawText 0x0000000104958be8 $s8drawText0B5ImageC0A06inSizeSo10CGImageRefaSgSo6CGSizeV_tKF + 420
[19:07:08]: ▸ 9 drawText 0x0000000104954294 main + 1612
[19:07:08]: ▸ 10 dyld 0x000000018843fe50 start + 2544
[19:07:08]: ▸ )
[19:07:08]: ▸ libc++abi: terminating with uncaught exception of type NSException
```

The crash happens on the following line in the code:

https://github.com/Automattic/drawText/blob/abd7d1cb4044671fb6fdc6799ec5a440ed8c4c8f/Sources/drawText/CoreTextStack.swift#L71-L73

The incorrect range causing the OutOfBounds is calculated at this place in the code:

https://github.com/Automattic/drawText/blob/abd7d1cb4044671fb6fdc6799ec5a440ed8c4c8f/Sources/drawText/CoreTextStack.swift#L110-L112

The file used for the `html=` argument was the following one:

https://github.com/wordpress-mobile/WordPress-Android/blob/b1cc0c748cf1e2c2d48302f547d7017d5b968859/fastlane/metadata/android/ar/screenshot_2.txt

My guess for the most likely reason for this crash is an issue with string encodings conversion, with the `fastlane/metadata/android/ar/screenshot_2.txt` file containing Arabic characters as UTF-8 Unicode text, but the Obj-C code manipulating `NSString` and `NS{Mutable}AttributedString` instances rely on UTF-16, and it's likely that the `NSRange` is expressed in one encoding space but the `NSMutableAttributedString`'s content is using another.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.