[Clang/AMDGPU] Zero sized arrays not allowed in HIP device code. (#113470)
[llvm-project.git] / clang / test / Analysis / malloc-sizeof.cpp
blobc787a7648a2c2213296df39c3c1a93ef5814a6f2
1 // RUN: %clang_analyze_cc1 -analyzer-checker=unix.MallocSizeof -verify %s
3 #include <stddef.h>
5 void *malloc(size_t size);
6 void *calloc(size_t nmemb, size_t size);
7 void *realloc(void *ptr, size_t size);
8 void free(void *ptr);
10 struct A {};
11 struct B {};
13 void foo(unsigned int unsignedInt, unsigned int readSize) {
14 // Verify the checker is working as expected.
15 A* a = static_cast<A*>(malloc(sizeof(int))); // expected-warning {{Result of 'malloc' is converted to a pointer of type 'A', which is incompatible with sizeof operand type 'int'}}
16 free(a);
19 void bar() {
20 A *x = static_cast<A*>(calloc(10, sizeof(void*))); // expected-warning {{Result of 'calloc' is converted to a pointer of type 'A', which is incompatible with sizeof operand type 'void *'}}
21 // sizeof(void*) is compatible with any pointer.
22 A **y = static_cast<A**>(calloc(10, sizeof(void*))); // no-warning
23 free(x);
24 free(y);