[llvm] r365144 - llvm-c-test avoid calling malloc(0)

Andus Yu via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 4 07:36:34 PDT 2019


Author: andusy
Date: Thu Jul  4 07:36:34 2019
New Revision: 365144

URL: http://llvm.org/viewvc/llvm-project?rev=365144&view=rev
Log:
llvm-c-test avoid calling malloc(0)

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

Modified:
    llvm/trunk/tools/llvm-c-test/attributes.c

Modified: llvm/trunk/tools/llvm-c-test/attributes.c
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-c-test/attributes.c?rev=365144&r1=365143&r2=365144&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-c-test/attributes.c (original)
+++ llvm/trunk/tools/llvm-c-test/attributes.c Thu Jul  4 07:36:34 2019
@@ -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);
           }




More information about the llvm-commits mailing list