[NSURL URLByAppendingPathComponent] crashes with Emoji characters

Originator:neoneye
Number:rdar://15909578 Date Originated:01/25/2014
Status:Open Resolved:No
Product:iOS SDK Product Version:7.0
Classification:Crash Reproducible:Always
 
Summary:
I have noticed that when entering asian letters and emoji in my iOS app that my app is crashing. I have traced it down to URLByAppendingPathComponent and can reproduce the crash.

This may also crash on osx. I haven't checked.

Steps to Reproduce:
Run the attached snippet.

Expected Results:
I expect to run the attached snipped without it crashing.

Actual Results:
Run the attached snippet always triggers a crash.

Version:
iOS 7.0.4.
Xcode 5.0.2

Configuration:
The attached snippet crashes both on the device and in the iPhone simulator. 

It crashes in both the 32bit simulator and the 64bit simulator.

It crashes on my iPhone5S. 

--------------
//
//  main.m
//  Created by Simon Strandgaard on 2014/01/25.
//  Copyright 2014 opcoders.com. All rights reserved.
//

#import <Foundation/Foundation.h>

NSData *DataFromHex(NSString* hexString) {
	NSString *s = [hexString stringByReplacingOccurrencesOfString:@" " withString:@""];
	NSMutableData *data= [[NSMutableData alloc] init];
	unsigned char whole_byte;
	char byte_chars[3] = {'\0','\0','\0'};
	for (int i = 0; i < ([s length] / 2); i++) {
		byte_chars[0] = [s characterAtIndex:i*2];
		byte_chars[1] = [s characterAtIndex:i*2+1];
		whole_byte = strtol(byte_chars, NULL, 16);
		[data appendBytes:&whole_byte length:1];
	}
	return data;
}

int main(int argc, char *argv[]) {
    @autoreleasepool {
		NSString *hex = @"fffe3100 3cd87adf 3dd891dc 3dd83ade 3dd868dc 3dd88bdc 3dd82e00 74007200 64007200";
		NSString *evilPathComponent = [[NSString alloc] initWithData:DataFromHex(hex) encoding:NSUTF16StringEncoding];
		NSURL *url = [NSURL URLWithString:@"file:///dir0/dir1"];
		NSURL *result = [url URLByAppendingPathComponent:evilPathComponent]; // EXC_BAD_ACCESS
		NSLog(@"We never gets here: %@", result);
		return EXIT_SUCCESS;
	}
}

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!