堆栈溢出 - [NSString(NSURLUtilities)stringByAddingPercentEncodingWithAllowedCharacters:]

时间:2017-06-01 13:50:39

标签: ios nsstring ios8.2

来自http://www.openradar.me/20404230

  

方法 - [NSString(NSURLUtilities)stringByAddingPercentEncodingWithAllowedCharacters:]有一个堆栈溢出问题,可以使用包含象形文字的一些字符串重现。在这些情况下,__ stack_chk_fail将在构建arm64架构时中止应用程序,并且在构建armv7时堆栈将被破坏。

来自https://github.com/PavelTretyakov/nsstring-crash的示例将在iOS 8.2上崩溃:

distance

来自https://gist.github.com/clowwindy/0d800f07a5e95e5c4dd0的示例将在iOS 8.1上崩溃:

NSString *str = @"/Users/zaryanov/Movies/rootfolder/시티 오브 히어로 (City of Heroes)/로니 리 가드너 (1961년부터 2010년까지)는 1985 년에 살인죄로 사형을받은 유타 주에서 총살형 된 미국의 악당이었다. 1984 년에 그는 솔트 레이크 시티에서 강도 동안 바텐더를 살해.m4v";
str = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]];

来自https://github.com/Alamofire/Alamofire/issues/206的示例将在iOS 7上崩溃到iOS 8.2:

NSString *base64String = @"5a+55LqOTGF1bmNoZXLov5nnsbvkuqflk4HmnaXor7TvvIzlroPlvojlrrnmmJPorqnkurrku6zpmbflhaXov5nmmK/lt6Xlhbfov5jmmK/lubPlj7DnmoTkuonmiafkuK3jgILkuI3ov4flnKjmnY7mtpvnnIvmnaXvvIzov5nnp43kuonmiaflrozlhajmmK/kuIDkuKrkvKrlkb3popjvvIzlm6DkuLrkuIDmrL7kuqflk4HnlKjnmoTkurrlpJrkuoblroPoh6rnhLblsLHmmK/lubPlj7DvvIznlKjnmoTkurrlsJHkuoblroPku4DkuYjpg73kuI3mmK/jgILln7rkuo7mraTvvIzmnY7mtpvlhbblrp7lubbmsqHmnInov4flpJrnmoTljrvogIPomZFBUFVTIExhdW5jaGVy6KaB5YGa5bmz5Y+w6L+Y5piv5bel5YW377yM5LuW5oOz55qE5pu05aSa55qE5piv5aaC5L2V6Kej5Yaz55So5oi355qE6Zeu6aKY44CC5L2c5Li65LiA5Liq5Y2z55SoaU9T5Y+I55SoQW5kcm9pZOeahOeUqOaIt++8jOaIkeacrOS6uueahOS4gOS4quS9k+S8muWwseaYr2lQaG9uZeS8mue7meS6uuS4gOenjeS9oOi2iueUqOi2iuinieW+l+Wug+WlveeUqOeahOaEn+inie+8jOS9hkFuZHJvaWTlsLHkuI3kvJrjgILmiYDku6VBUFVTIExhdW5jaGVy546w5Zyo5bCx6KaB6Kej5Yaz6L+Z5Liq6Zq+6aKY77yM6K6pQW5kcm9pZOWPmOW+l+WlveeUqOOAgui/meS5n+aYr+S4uuS9leadjua2m+S8muivtOiHquW3seWBmueahOS4jeaYr+S4gOS4qkxhdW5jaGVy6ICM5piv5LiA5aWX4oCc55So5oi357O757uf4oCd44CC";
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
NSString *str = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
str = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];

1 个答案:

答案 0 :(得分:0)

PrideChung在https://github.com/Alamofire/Alamofire/issues/206中提供了内存问题解决方法的识别。随后由cnoon给出以下描述:

  

经过大量调试后,我能够使用iPhone 4S和iPhone 5模拟器跟踪此问题,仅发生在iOS 8.1和8.2的Alamofire上。它是100%可重复的,但是根据传入的中文字符串的大小以不同的方式崩溃。它总是某种形式的malloc错误。

     

[...]

     

由于iOS 8.1和8.2中的内部错误导致转义需要批处理。编码超过几百个中文字符会导致各种malloc错误崩溃。为了避免这个问题,必须使用批处理进行编码。

我的实际解决方法受AlamoFire的启发,是:

let str = String(repeating: "一二三四五六七八九十", count: 2_000)
var allowedCharacterSet = CharacterSet.urlQueryAllowed
allowedCharacterSet.remove(charactersIn: ":#[]@!$&'()*+,;=")
_ = str.addingPercentEncoding(withAllowedCharacters: allowedCharacterSet)