1 | // RUN: %check_clang_tidy %s cppcoreguidelines-no-malloc %t \ |
2 | // RUN: -config='{CheckOptions: \ |
3 | // RUN: {cppcoreguidelines-no-malloc.Allocations: "::malloc;::align_malloc;::calloc",\ |
4 | // RUN: cppcoreguidelines-no-malloc.Reallocations: "::realloc;::align_realloc",\ |
5 | // RUN: cppcoreguidelines-no-malloc.Deallocations: "::free;::align_free"}}' \ |
6 | // RUN: -- |
7 | |
8 | using size_t = __SIZE_TYPE__; |
9 | |
10 | void *malloc(size_t size); |
11 | void *align_malloc(size_t size, unsigned short alignment); |
12 | void *calloc(size_t num, size_t size); |
13 | void *realloc(void *ptr, size_t size); |
14 | void *align_realloc(void *ptr, size_t size, unsigned short alignment); |
15 | void free(void *ptr); |
16 | void *align_free(void *ptr); |
17 | |
18 | void malloced_array() { |
19 | int *array0 = (int *)malloc(size: sizeof(int) * 20); |
20 | // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: do not manage memory manually; consider a container or a smart pointer [cppcoreguidelines-no-malloc] |
21 | |
22 | int *zeroed = (int *)calloc(num: 20, size: sizeof(int)); |
23 | // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: do not manage memory manually; consider a container or a smart pointer [cppcoreguidelines-no-malloc] |
24 | |
25 | int *aligned = (int *)align_malloc(size: 20 * sizeof(int), alignment: 16); |
26 | // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: do not manage memory manually; consider a container or a smart pointer [cppcoreguidelines-no-malloc] |
27 | |
28 | // reallocation memory, std::vector shall be used |
29 | char *realloced = (char *)realloc(ptr: array0, size: 50 * sizeof(int)); |
30 | // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: do not manage memory manually; consider std::vector or std::string [cppcoreguidelines-no-malloc] |
31 | |
32 | char *align_realloced = (char *)align_realloc(ptr: aligned, size: 50 * sizeof(int), alignment: 16); |
33 | // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: do not manage memory manually; consider std::vector or std::string [cppcoreguidelines-no-malloc] |
34 | |
35 | // freeing memory the bad way |
36 | free(ptr: realloced); |
37 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not manage memory manually; use RAII [cppcoreguidelines-no-malloc] |
38 | |
39 | align_free(ptr: align_realloced); |
40 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not manage memory manually; use RAII [cppcoreguidelines-no-malloc] |
41 | |
42 | // check if a call to malloc as function argument is found as well |
43 | free(ptr: malloc(size: 20)); |
44 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not manage memory manually; use RAII [cppcoreguidelines-no-malloc] |
45 | // CHECK-MESSAGES: :[[@LINE-2]]:8: warning: do not manage memory manually; consider a container or a smart pointer [cppcoreguidelines-no-malloc] |
46 | } |
47 | |
48 | /// newing an array is still not good, but not relevant to this checker |
49 | void newed_array() { |
50 | int *new_array = new int[10]; // OK(1) |
51 | } |
52 | |
53 | void arbitrary_call() { |
54 | // we dont want every function to raise the warning even if malloc is in the name |
55 | malloced_array(); // OK(2) |
56 | |
57 | // completely unrelated function call to malloc |
58 | newed_array(); // OK(3) |
59 | } |
60 | |