[swift-users] Runtime crash caused by (NS)CharacterSet.hasMember?

Doug Hill swiftusers at breaqz.com
Tue Jun 13 12:14:44 CDT 2017


I tried your calls to NSFont in Objective-C, and it also crashes. Looking at the stack trace, I see that it crashes in CFCharacterSetHasMemberInPlane. I see documentation for thePlane parameter of this function says:

The valid value range is from 0 to 16.  If the value is outside of the valid plane number range, the behavior is undefined.

I guess randomly crashing counts as undefined behavior.
While it would be great if this function was more robust, Apple is definitely saying Don’t Do That.

Doug Hill
https://github.com/djfitz/SFFontFeatures <https://github.com/djfitz/SFFontFeatures>


> On Jun 13, 2017, at 9:42 AM, Jens Persson via swift-users <swift-users at swift.org> wrote:
> 
> PS
> There are only 17 planes in the (current?) Unicode standard, but I'm assuming that the CharacterSet.hasMember(inPlane:) shouldn't (randomly?) crash for UInt8 values other than 0 ... 16?
> It also seems strange that it reproducibly crashes or not depending on which specific font I use.
> 
> 
> On Tue, Jun 13, 2017 at 4:00 PM, Jens Persson <jens at bitcycle.com <mailto:jens at bitcycle.com>> wrote:
> The below program demonstrates a possible bug that I encountered.
> Can anyone else reproduce my results (as explained in the comments)?
> If so, is it a Swift or Cocoa bug (I haven't tried it in Objective C)?
> 
> import AppKit
> 
> extension NSFont {
>     var planesWithMembers: [UInt8] {
>         return (UInt8(0) ... UInt8(255)).filter {
>             // The follwing line will crash (EXC_BAD_ACCESS) for some fonts:
>             coveredCharacterSet.hasMember(inPlane: $0)
>             // I have tested it with Xcode 8.3.3 and Xcode 9 beta 1, both
>             // Swift 3 and 4, and they all reproduce the crash.
>             // macOS 10.12.5
>         }
>     }
> }
> 
> // Examples that will not cause the crash:
> // let fontName = "AlBayan"
> // let fontName = "AppleBraille"
> // let fontName = "Chalkboard"
> // let fontName = "Helvetica"
> // let fontName = "Zapfino"
> 
> // Examples that will cause the crash:
> // let fontName = "AppleColorEmoji"
> // let fontName = "AppleSymbols"
> let fontName = "Baskerville"
> // let fontName = "Geneva"
> // let fontName = "SnellRoundhand"
> 
> let font = NSFont(name: fontName, size: 12)!
> let result = font.planesWithMembers
> print("Planes that contains members:", result)
> 
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170613/37b82169/attachment.html>


More information about the swift-users mailing list