iOS: Make room for zero-terminator when building argv from NSProcessInfo

[NSString lengthOfBytesUsingEncoding] only returns the number of bytes
required for the actual string, not including the zero terminator, so
when we then used cStringUsingEncoding to fill the malloced buffer with
data, we overwrote the byte after our buffer with 0, resulting in random
and hard to reproduce crashes at application startup, seemingly depending
on the application name.

Change-Id: I35d261bea5924e917475b0270bfa280bfb0c787a
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@digia.com>
This commit is contained in:
Tor Arne Vestbø 2014-10-24 17:12:35 +02:00
parent 0c482869fb
commit 27a321e1ed

View File

@ -259,10 +259,16 @@ static void __attribute__((noinline, noreturn)) user_main_trampoline()
NSArray *arguments = [[NSProcessInfo processInfo] arguments];
int argc = arguments.count;
char **argv = new char*[argc];
for (int i = 0; i < argc; ++i) {
NSString *arg = [arguments objectAtIndex:i];
argv[i] = reinterpret_cast<char *>(malloc([arg lengthOfBytesUsingEncoding:[NSString defaultCStringEncoding]]));
strcpy(argv[i], [arg cStringUsingEncoding:[NSString defaultCStringEncoding]]);
NSStringEncoding cStringEncoding = [NSString defaultCStringEncoding];
unsigned int bufferSize = [arg lengthOfBytesUsingEncoding:cStringEncoding] + 1;
argv[i] = reinterpret_cast<char *>(malloc(bufferSize));
if (![arg getCString:argv[i] maxLength:bufferSize encoding:cStringEncoding])
qFatal("Could not convert argv[%d] to C string", i);
}
int exitCode = qtmn(argc, argv);