alexisakers / HTMLString

Escape and unescape HTML entities in Swift
MIT License
170 stars 69 forks source link

sporadic crashes on iOS 13 devices when using the removingHTMLEntities property #27

Closed tamirbb closed 3 years ago

tamirbb commented 4 years ago

This issue is iOS 13 specific.

We use the removingHTMLEntities property on long html strings. We see this crash on rare occasions - about 800 cases per 1 million sessions. It also seems that similar calls on the same string would not result in a crash. So we believe this is a threading issue.

Crashlytics data:

crash_info: Fatal error: file /AppleInternal/BuildRoot/Library/Caches/com.apple.xbs/Binaries/swiftlang/install/TempContent/Objects/BNI_iOS/swift-macosx-x86_64/stdlib/public/core/8/UnsafeBufferPointer.swift, line 886

Stack trace:

Crashed: com.apple.main-thread
0  libswiftCore.dylib             0x1932103bc closure #1 in closure #1 in closure #1 in _assertionFailure(_:_:file:line:flags:) + 456
1  libswiftCore.dylib             0x19320f834 _assertionFailure(_:_:file:line:flags:) + 472
2  libswiftCore.dylib             0x19320fb3c _fatalErrorMessage(_:_:file:line:flags:) + 44
3  libswiftCore.dylib             0x19335aa08 _scalarAlign(_:_:) + 1010
4  libswiftCore.dylib             0x19335a21c _stringCompareInternal(_:_:expecting:) + 612
5  HTMLString                     0x103345f18 $sSS10HTMLStringE18removeHTMLEntitiesyyF + 236
6  HTMLString                     0x103346ebc $sSS10HTMLStringE21addingUnicodeEntitiesSSvgTm + 40
RajeshBudhiraja commented 4 years ago

Facing same.

OlegKetrar commented 4 years ago

Same problem

OlegKetrar commented 4 years ago

I found that it can be related to NSString-backed strings. For example:

let swiftStr = "string with html entities"
_ = swiftStr.removingHTMLEntities // no crash

but:

let nsStr = NSString("string with html entities") 
let str = nsStr as String
_ = str.removingHTMLEntities // crash!

To solve this we can force Swift to convert string storage by any mutation on a String, for example:

let nsStr = NSString("string with html entities") 
var str = nsStr as String
str.append("a")
str.removeLast(1)
_ = str.removingHTMLEntities // no crash
tamirbb commented 4 years ago

@OlegKetrar Thanks for posting, It's NSString related in my case as well, I'll try your workaround. Thanks!

JCSooHwanCho commented 4 years ago

I tried it but It didn't work in my case

alexisakers commented 4 years ago

@JCSooHwanCho made a PR that changes the logic to no longer use indices. Can y'all check if updating to 6.0.0 fixes the issue?

JCSooHwanCho commented 3 years ago

@alexaubry It may fixed in 6.0.1. Any crash doesn't reported yet.