我注意到XCode 8中AddressSanitizer有趣的行爲。在一種情況下,它導致捕獲斷點:AddressSanitizer:地址上的堆緩衝區溢出,但我真的不明白爲什麼:NSData轉換堆緩衝區溢出
char * buffer = malloc(length);
memset(buffer, 0, length);
[output getBytes:buffer
length:length];
stringOutput = [NSString stringWithUTF8String:buffer]; // here is crash
相同於:
stringOutput = [NSString stringWithUTF8String:output.bytes];
但一切罰款情況:
stringOutput = [[NSString alloc] initWithData:output
encoding:NSUTF8StringEncoding];
而且經過一些實驗,我已經看到了,如果我們加「0」到緩衝區一切都會精細的結尾:
char * buffer = malloc(length + 1);
memset(buffer, 0, length + 1);
[output getBytes:buffer
length:length];
stringOutput = [NSString stringWithUTF8String:buffer];
這種行爲不可預料的我,因爲我使用stringWithUTF8String:output.bytes現場代碼十幾次沒有任何問題...所以我錯了?
'[NSString stringWithUTF8String:buffer]'讀取字節,直到找到NUL終結符。如果分配和填充的內存中沒有零字節,則將讀取未定義的內存。 –
但爲什麼stringOutput = [NSString stringWithUTF8String:output.bytes];導致崩潰?我認爲可可巧妙地解決了這種問題? – toohtik
因爲該方法*無法知道*現在許多字節被定義或有效。 –