[cfe-commits] [PATCH 1/1] Checking zero byte allocation for realloc() and calloc().
Benjamin Kramer
benny.kra at googlemail.com
Sun Nov 13 03:05:38 PST 2011
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).
> 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.
-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
More information about the cfe-commits
mailing list