diff options
author | Ted Kremenek <kremenek@apple.com> | 2012-01-03 23:43:13 +0000 |
---|---|---|
committer | Ted Kremenek <kremenek@apple.com> | 2012-01-03 23:43:13 +0000 |
commit | 134a83a799fac622ffa781efcc934d27f933d37e (patch) | |
tree | 0ce5d9427079d7f55bc8210a77db1c19e58220b0 /clang/test/Analysis/unix-fns.c | |
parent | 6aeecf1791544e0b7781ee5ef8fa83419eac8c36 (diff) | |
download | bcm5719-llvm-134a83a799fac622ffa781efcc934d27f933d37e.tar.gz bcm5719-llvm-134a83a799fac622ffa781efcc934d27f933d37e.zip |
Enhance UnixAPIChecker to also warn about zero-sized allocations to calloc() and realloc(). Patch by Cyril Roelandt!
llvm-svn: 147500
Diffstat (limited to 'clang/test/Analysis/unix-fns.c')
-rw-r--r-- | clang/test/Analysis/unix-fns.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/clang/test/Analysis/unix-fns.c b/clang/test/Analysis/unix-fns.c index cf5ca810de3..86b95c60e14 100644 --- a/clang/test/Analysis/unix-fns.c +++ b/clang/test/Analysis/unix-fns.c @@ -9,7 +9,9 @@ typedef __darwin_pthread_once_t pthread_once_t; int pthread_once(pthread_once_t *, void (*)(void)); typedef long unsigned int __darwin_size_t; typedef __darwin_size_t size_t; +void *calloc(size_t, size_t); void *malloc(size_t); +void *realloc(void *, size_t); typedef void (^dispatch_block_t)(void); typedef long dispatch_once_t; @@ -66,3 +68,33 @@ void pr2899_nowarn(size_t size) { foo[i] = 0; } } +void test_calloc(void) { + char *foo = calloc(0, 42); // expected-warning{{Call to 'calloc' has an allocation size of 0 bytes}} + for (unsigned i = 0; i < 100; i++) { + foo[i] = 0; + } +} +void test_calloc2(void) { + char *foo = calloc(42, 0); // expected-warning{{Call to 'calloc' has an allocation size of 0 bytes}} + for (unsigned i = 0; i < 100; i++) { + foo[i] = 0; + } +} +void test_calloc_nowarn(size_t nmemb, size_t size) { + char *foo = calloc(nmemb, size); // no-warning + for (unsigned i = 0; i < 100; i++) { + foo[i] = 0; + } +} +void test_realloc(char *ptr) { + char *foo = realloc(ptr, 0); // expected-warning{{Call to 'realloc' has an allocation size of 0 bytes}} + for (unsigned i = 0; i < 100; i++) { + foo[i] = 0; + } +} +void test_realloc_nowarn(char *ptr, size_t size) { + char *foo = realloc(ptr, size); // no-warning + for (unsigned i = 0; i < 100; i++) { + foo[i] = 0; + } +} |