This is embarrassing…

On 4 Oct 2015, at 5:39 PM, Fritz Anderson <fri...@manoverboard.org> wrote:
> 
> func OSTypeFor(code: UInt32,
>    encoding: UInt = NSMacOSRomanStringEncoding)
>    throws -> String
> {
>    let codePtr = UnsafeMutablePointer<UInt32>.alloc(1)
>    codePtr.initializeFrom([code])
>    defer { codePtr.destroy() }
> 
>    let fourChars = UnsafeMutablePointer<CChar>(codePtr)

/*** OOPS ***/

>    if let retval = String(CString: fourChars,
>        encoding: encoding)
>    {
>        return retval
>    }
>    else { throw OSTypeError.BadStringFormat }
> }
> 

fourChars gets passed to String(CString:encoding:) without a fifth character to 
provide the NUL to terminate the C String. I’m surprised it was well-behaved in 
my playground, but something _will_ go wrong in the mean time. fourChars should 
be an unsafe buffer of five or six CChar, with [4] zeroed-out.

Maybe I was rescued by the page being larger than four bytes and zeroed-out, 
but an aggressive address sanitizer would cry foul.

        — F


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to