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

Cyril Roelandt tipecaml at gmail.com
Sat Nov 12 17:52:07 PST 2011


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.

Checking both arguments of calloc() makes the code a little bit ugly, so 
I'll probably need to give it some more work.

You may find a test case attached to this mail :

$ 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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: zero_byte_allocation.patch
Type: text/x-patch
Size: 4422 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20111113/89dbd296/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: alloc.c
Type: text/x-csrc
Size: 597 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20111113/89dbd296/attachment.c>


More information about the cfe-commits mailing list