From: Andus Yu Date: Thu, 4 Jul 2019 14:36:34 +0000 (+0000) Subject: llvm-c-test avoid calling malloc(0) X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4c4c53bae2899715d71aed85fcf018a2fc073f00;p=llvm 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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@365144 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/tools/llvm-c-test/attributes.c b/tools/llvm-c-test/attributes.c index 826cb094e22..487769f94db 100644 --- a/tools/llvm-c-test/attributes.c +++ b/tools/llvm-c-test/attributes.c @@ -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); }