Browse Source

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.
Thomas Van Lenten 5 years ago
parent
commit
065fa2f395
1 changed files with 26 additions and 17 deletions
  1. 26 17
      objectivec/Tests/GPBUnknownFieldSetTest.m

+ 26 - 17
objectivec/Tests/GPBUnknownFieldSetTest.m

@@ -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];
 }