[cfe-commits] [PATCH 1/1] Checking zero byte allocation for realloc() and calloc().

Cyril Roelandt tipecaml at gmail.com
Sat Nov 19 21:07:50 PST 2011


On 11/13/2011 12:05 PM, Benjamin Kramer wrote:
>
> On 13.11.2011, at 02:52, Cyril Roelandt wrote:
>
>> Hello,
>>
>> Currently, clang can check zero byte allocations for the malloc function. I think it should also warn programmers about zero byte allocations for the calloc and realloc functions.
>>
>> Attached is a patch that implements this idea. It checks either the first argument of malloc(), or the second argument of realloc(), or both arguments of calloc(), looking for a value constrained to 0.
>
> Nice! Thanks for working on this.
>
>> Checking both arguments of calloc() makes the code a little bit ugly, so I'll probably need to give it some more work.
>
> How about:
> - Use 3 separate methods for the 3 functions (CheckMallocZero/CheckCallocZero/CheckReallocZero) where the prototype is checked.
> - Factor the actual 0-checking and report emission into a helper method.
> - Call the new helper method on the argument (or both arguments in the case of calloc).
>

The code should look a little bit better now. Three different functions 
are used. The 0-checking and report emission are now two different 
functions, for I thought they served a different purpose, and that the 
code would be easier to understand that way.

>> You may find a test case attached to this mail :
>
> It would be nice to add these tests to test/Analysis/unix-fns.c so they're part of our regression tests. Just copy the code in there and add comments to validate the warning message, the format is straight forward.
>

Done ! May I ask why we use a for loop in the malloc test ? I've done 
the same for calloc and realloc, but did not really get it.

The attached patch contains both the tests and the changes to the Unix 
API checker.

Cyril.

> -Ben
>
>>
>> $ clang --analyze -c -o alloc alloc.c
>> /tmp/alloc.c:29:16: warning: Call to 'malloc' has an allocation size of 0 bytes
>>     int *ptr = malloc(0);
>>                ^      ~
>> /tmp/alloc.c:36:16: warning: Call to 'calloc' has an allocation size of 0 bytes
>>     int *ptr = calloc(0, sizeof(int));
>>                ^      ~
>> /tmp/alloc.c:43:16: warning: Call to 'calloc' has an allocation size of 0 bytes
>>     int *ptr = calloc(42, 0);
>>                ^          ~
>> /tmp/alloc.c:50:16: warning: Call to 'realloc' has an allocation size of 0 bytes
>>     int *ptr = realloc(NULL, 0);
>>                ^             ~
>>
>> Looking forward to hearing from you,
>> Cyril Roelandt.
>> <zero_byte_allocation.patch><alloc.c>_______________________________________________
>> cfe-commits mailing list
>> cfe-commits at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: calloc-realloc.patch
Type: text/x-patch
Size: 8284 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20111120/685905d9/attachment.bin>


More information about the cfe-commits mailing list