File: no-malloc-custom.cpp

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (59 lines) | stat: -rw-r--r-- 2,858 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// RUN: %check_clang_tidy %s cppcoreguidelines-no-malloc %t \
// RUN: -config='{CheckOptions: \
// RUN:  [{key: cppcoreguidelines-no-malloc.Allocations, value: "::malloc;::align_malloc;::calloc"},\
// RUN:   {key: cppcoreguidelines-no-malloc.Reallocations, value: "::realloc;::align_realloc"},\
// RUN:   {key: cppcoreguidelines-no-malloc.Deallocations, value: "::free;::align_free"}]}' \
// RUN: --

using size_t = __SIZE_TYPE__;

void *malloc(size_t size);
void *align_malloc(size_t size, unsigned short alignment);
void *calloc(size_t num, size_t size);
void *realloc(void *ptr, size_t size);
void *align_realloc(void *ptr, size_t size, unsigned short alignment);
void free(void *ptr);
void *align_free(void *ptr);

void malloced_array() {
  int *array0 = (int *)malloc(sizeof(int) * 20);
  // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: do not manage memory manually; consider a container or a smart pointer [cppcoreguidelines-no-malloc]

  int *zeroed = (int *)calloc(20, sizeof(int));
  // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: do not manage memory manually; consider a container or a smart pointer [cppcoreguidelines-no-malloc]

  int *aligned = (int *)align_malloc(20 * sizeof(int), 16);
  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: do not manage memory manually; consider a container or a smart pointer [cppcoreguidelines-no-malloc]

  // reallocation memory, std::vector shall be used
  char *realloced = (char *)realloc(array0, 50 * sizeof(int));
  // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: do not manage memory manually; consider std::vector or std::string [cppcoreguidelines-no-malloc]

  char *align_realloced = (char *)align_realloc(aligned, 50 * sizeof(int), 16);
  // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: do not manage memory manually; consider std::vector or std::string [cppcoreguidelines-no-malloc]

  // freeing memory the bad way
  free(realloced);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not manage memory manually; use RAII [cppcoreguidelines-no-malloc]

  align_free(align_realloced);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not manage memory manually; use RAII [cppcoreguidelines-no-malloc]
  
  // check if a call to malloc as function argument is found as well
  free(malloc(20));
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not manage memory manually; use RAII [cppcoreguidelines-no-malloc]
  // CHECK-MESSAGES: :[[@LINE-2]]:8: warning: do not manage memory manually; consider a container or a smart pointer [cppcoreguidelines-no-malloc]
}

/// newing an array is still not good, but not relevant to this checker
void newed_array() {
  int *new_array = new int[10]; // OK(1)
}

void arbitrary_call() {
  // we dont want every function to raise the warning even if malloc is in the name
  malloced_array(); // OK(2)

  // completely unrelated function call to malloc
  newed_array(); // OK(3)
}