This is an archive of the discontinued LLVM Phabricator instance.

llvm-c-test avoid calling malloc(0)
ClosedPublic

Authored by andusy on Jun 25 2019, 1:33 PM.

Details

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.

Diff Detail

Event Timeline

andusy created this revision.Jun 25 2019, 1:33 PM

For a generic tool, this seems to be an okay fix; however, this is an API test driver. I would like to hear from other reviewers as to whether there is an intent to test the API for the AttrCount == 0 case. If there is, then I believe that allocation should not be attempted, and the API should work with a null pointer.

To preserve exercising of the list getter when the count is zero, I suggest:

int AttrCount = /*...*/;
LLVMAttributeRef *Attrs = 0;
if (AttrCount) {
  Attrs = /*...*/;
  assert(Attrs);
}
LLVMGet/*...*/;
free(Attrs);

Even on systems where malloc(0) does not return a null pointer, the getter functions could not have safely accessed an object via said pointer.

andusy updated this revision to Diff 207068.Jun 28 2019, 8:13 AM

Update to call LLVMGetAttributesAtIndex even when AttrCount is 0.

Thanks @andusy; this LGTM. I believe that you have had a good number of patches committed into the project already (rC356060, rC360900, rL364322, rC364462). Please go ahead with requesting commit access, and commit this patch when you are ready.

This revision is now accepted and ready to land.Jun 28 2019, 8:48 AM
This revision was automatically updated to reflect the committed changes.