]> granicus.if.org Git - llvm/commitdiff
llvm-c-test avoid calling malloc(0)
authorAndus Yu <andusyllvm@gmail.com>
Thu, 4 Jul 2019 14:36:34 +0000 (14:36 +0000)
committerAndus Yu <andusyllvm@gmail.com>
Thu, 4 Jul 2019 14:36:34 +0000 (14:36 +0000)
Summary:
As explained in 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.

Authored By: andusy

Reviewers: hubert.reinterpretcast, xingxue, jasonliu, daltenty, cebowleratibm

Reviewed By: hubert.reinterpretcast

Subscribers: mehdi_amini, dexonsmith, jsji, llvm-commits

Tags: LLVM

Differential Revision: https://reviews.llvm.org/D63788

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@365144 91177308-0d34-0410-b5e6-96231b3b80d8

tools/llvm-c-test/attributes.c

index 826cb094e226f64356ea197b64b2ebdbb476ac7b..487769f94dbcbac9ef79e04a362104e57bec0c37 100644 (file)
@@ -29,9 +29,12 @@ int llvm_test_function_attributes(void) {
     for (Idx = LLVMAttributeFunctionIndex, ParamCount = LLVMCountParams(F);
          Idx <= ParamCount; ++Idx) {
       int AttrCount = LLVMGetAttributeCountAtIndex(F, Idx);
-      LLVMAttributeRef *Attrs =
-          (LLVMAttributeRef *)malloc(AttrCount * sizeof(LLVMAttributeRef));
-      assert(Attrs);
+      LLVMAttributeRef *Attrs = 0;
+      if (AttrCount) {
+        Attrs =
+            (LLVMAttributeRef *)malloc(AttrCount * sizeof(LLVMAttributeRef));
+        assert(Attrs);
+      }
       LLVMGetAttributesAtIndex(F, Idx, Attrs);
       free(Attrs);
     }
@@ -61,9 +64,12 @@ int llvm_test_callsite_attributes(void) {
               ParamCount = LLVMCountParams(F);
                Idx <= ParamCount; ++Idx) {
             int AttrCount = LLVMGetCallSiteAttributeCount(I, Idx);
-            LLVMAttributeRef *Attrs = (LLVMAttributeRef *)malloc(
-                AttrCount * sizeof(LLVMAttributeRef));
-            assert(Attrs);
+            LLVMAttributeRef *Attrs = 0;
+            if (AttrCount) {
+              Attrs = (LLVMAttributeRef *)malloc(
+                  AttrCount * sizeof(LLVMAttributeRef));
+              assert(Attrs);
+            }
             LLVMGetCallSiteAttributes(I, Idx, Attrs);
             free(Attrs);
           }