- (NSData *)kd_AES128EncryptWithKey:(NSString *)key {
if (key.length == 0) {
return nil;
}
char keyPtr[kCCKeySizeAES128 + 1];
memset(keyPtr, 0, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
int diff = kCCKeySizeAES128 - (dataLength % kCCKeySizeAES128);
NSUInteger newSize = 0;
if (diff > 0) {
newSize = dataLength + diff;
}
char dataPtr[newSize];
memcpy(dataPtr, [self bytes], [self length]); // crash EXC_BAD_ACCESS
...
free(buffer);
return nil;
}
in this crash: newSize = 800016 dataSize = 800001
When i make AES128 encrypt for NSData(800001 bytes), crash in memcry(), but if the data is smaller
, it work normal
Could someone help me?
your dataPtr
is allocated in the stack which always has a certain limit. If you are dealing with big chunks of memory, then use heap instead:
char *dataPtr = (char *)malloc(newSize);
and don't forget to free it later
If diff <= 0
newSize
is 0
so no memory will be aqllocated to dataPtr
: char dataPtr[newSize];
so the memcpy
will crash.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.