Remove use of VLA.

VLAs complicate static analysis and bloat stack size. Replace VLA allocation
with calls to malloc and free.  This will alos the code to build with -Wvla.
This commit is contained in:
Thomas Van Lenten 2019-12-10 16:24:31 -05:00
parent 53a814a0ee
commit 065fa2f395

View File

@ -156,25 +156,34 @@
GPBUnknownFieldSet* bizarroFields =
[[[GPBUnknownFieldSet alloc] init] autorelease];
NSUInteger count = [unknownFields_ countOfFields];
int32_t tags[count];
[unknownFields_ getTags:tags];
for (NSUInteger i = 0; i < count; ++i) {
int32_t tag = tags[i];
GPBUnknownField* field = [unknownFields_ getField:tag];
if (field.varintList.count == 0) {
// Original field is not a varint, so use a varint.
GPBUnknownField* varintField =
[[[GPBUnknownField alloc] initWithNumber:tag] autorelease];
[varintField addVarint:1];
[bizarroFields addField:varintField];
} else {
// Original field *is* a varint, so use something else.
GPBUnknownField* fixed32Field =
[[[GPBUnknownField alloc] initWithNumber:tag] autorelease];
[fixed32Field addFixed32:1];
[bizarroFields addField:fixed32Field];
int32_t *tags = malloc(count * sizeof(int32_t));
if (!tags) {
XCTFail(@"Failed to make scratch buffer for testing");
return [NSData data];
}
@try {
[unknownFields_ getTags:tags];
for (NSUInteger i = 0; i < count; ++i) {
int32_t tag = tags[i];
GPBUnknownField* field = [unknownFields_ getField:tag];
if (field.varintList.count == 0) {
// Original field is not a varint, so use a varint.
GPBUnknownField* varintField =
[[[GPBUnknownField alloc] initWithNumber:tag] autorelease];
[varintField addVarint:1];
[bizarroFields addField:varintField];
} else {
// Original field *is* a varint, so use something else.
GPBUnknownField* fixed32Field =
[[[GPBUnknownField alloc] initWithNumber:tag] autorelease];
[fixed32Field addFixed32:1];
[bizarroFields addField:fixed32Field];
}
}
}
@finally {
free(tags);
}
return [bizarroFields data];
}