[PATCH] D63788: llvm-c-test avoid calling malloc(0)

Andus Yu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 25 13:36:03 PDT 2019


andusy created this revision.
andusy added reviewers: hubert.reinterpretcast, xingxue, jasonliu, daltenty, cebowleratibm.
andusy added a project: LLVM.
Herald added subscribers: jsji, dexonsmith, mehdi_amini.

As explained in D63668 <https://reviews.llvm.org/D63668>, malloc(0) could return a null pointer. llvm-c-test does not handle this case correctly. Instead of calling malloc(0), avoid the operation altogether.


Repository:
  rL LLVM

https://reviews.llvm.org/D63788

Files:
  llvm/tools/llvm-c-test/attributes.c


Index: llvm/tools/llvm-c-test/attributes.c
===================================================================
--- llvm/tools/llvm-c-test/attributes.c
+++ llvm/tools/llvm-c-test/attributes.c
@@ -29,11 +29,13 @@
     for (Idx = LLVMAttributeFunctionIndex, ParamCount = LLVMCountParams(F);
          Idx <= ParamCount; ++Idx) {
       int AttrCount = LLVMGetAttributeCountAtIndex(F, Idx);
-      LLVMAttributeRef *Attrs =
-          (LLVMAttributeRef *)malloc(AttrCount * sizeof(LLVMAttributeRef));
-      assert(Attrs);
-      LLVMGetAttributesAtIndex(F, Idx, Attrs);
-      free(Attrs);
+      if (AttrCount) {
+        LLVMAttributeRef *Attrs =
+            (LLVMAttributeRef *)malloc(AttrCount * sizeof(LLVMAttributeRef));
+        assert(Attrs);
+        LLVMGetAttributesAtIndex(F, Idx, Attrs);
+        free(Attrs);
+      }
     }
     F = LLVMGetNextFunction(F);
   }
@@ -61,11 +63,13 @@
               ParamCount = LLVMCountParams(F);
                Idx <= ParamCount; ++Idx) {
             int AttrCount = LLVMGetCallSiteAttributeCount(I, Idx);
-            LLVMAttributeRef *Attrs = (LLVMAttributeRef *)malloc(
-                AttrCount * sizeof(LLVMAttributeRef));
-            assert(Attrs);
-            LLVMGetCallSiteAttributes(I, Idx, Attrs);
-            free(Attrs);
+            if (AttrCount) {
+              LLVMAttributeRef *Attrs = (LLVMAttributeRef *)malloc(
+                  AttrCount * sizeof(LLVMAttributeRef));
+              assert(Attrs);
+              LLVMGetCallSiteAttributes(I, Idx, Attrs);
+              free(Attrs);
+            }
           }
         }
       }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D63788.206515.patch
Type: text/x-patch
Size: 1607 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190625/974aa255/attachment.bin>


More information about the llvm-commits mailing list