1*89a1d03eSRichard // RUN: %check_clang_tidy %s bugprone-misplaced-pointer-arithmetic-in-alloc %t
2*89a1d03eSRichard 
3*89a1d03eSRichard typedef __typeof(sizeof(int)) size_t;
4*89a1d03eSRichard void *malloc(size_t);
5*89a1d03eSRichard void *alloca(size_t);
6*89a1d03eSRichard void *calloc(size_t, size_t);
7*89a1d03eSRichard void *realloc(void *, size_t);
8*89a1d03eSRichard 
bad_malloc(int n)9*89a1d03eSRichard void bad_malloc(int n) {
10*89a1d03eSRichard   char *p = (char *)malloc(n) + 10;
11*89a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: arithmetic operation is applied to the result of malloc() instead of its size-like argument
12*89a1d03eSRichard   // CHECK-FIXES: char *p = (char *)malloc(n + 10);
13*89a1d03eSRichard 
14*89a1d03eSRichard   p = (char *)malloc(n) - 10;
15*89a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: arithmetic operation is applied to the result of malloc() instead of its size-like argument
16*89a1d03eSRichard   // CHECK-FIXES: p = (char *)malloc(n - 10);
17*89a1d03eSRichard 
18*89a1d03eSRichard   p = (char *)malloc(n) + n;
19*89a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: arithmetic operation is applied to the result of malloc() instead of its size-like argument
20*89a1d03eSRichard   // CHECK-FIXES: p = (char *)malloc(n + n);
21*89a1d03eSRichard 
22*89a1d03eSRichard   p = (char *)malloc(n) - (n + 10);
23*89a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: arithmetic operation is applied to the result of malloc() instead of its size-like argument
24*89a1d03eSRichard   // CHECK-FIXES: p = (char *)malloc(n - (n + 10));
25*89a1d03eSRichard 
26*89a1d03eSRichard   p = (char *)malloc(n) - n + 10;
27*89a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: arithmetic operation is applied to the result of malloc() instead of its size-like argument
28*89a1d03eSRichard   // CHECK-FIXES: p = (char *)malloc(n - n) + 10;
29*89a1d03eSRichard   // FIXME: should be p = (char *)malloc(n - n + 10);
30*89a1d03eSRichard }
31*89a1d03eSRichard 
bad_alloca(int n)32*89a1d03eSRichard void bad_alloca(int n) {
33*89a1d03eSRichard   char *p = (char *)alloca(n) + 10;
34*89a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: arithmetic operation is applied to the result of alloca() instead of its size-like argument
35*89a1d03eSRichard   // CHECK-FIXES: char *p = (char *)alloca(n + 10);
36*89a1d03eSRichard }
37*89a1d03eSRichard 
bad_realloc(char * s,int n)38*89a1d03eSRichard void bad_realloc(char *s, int n) {
39*89a1d03eSRichard   char *p = (char *)realloc(s, n) + 10;
40*89a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: arithmetic operation is applied to the result of realloc() instead of its size-like argument
41*89a1d03eSRichard   // CHECK-FIXES: char *p = (char *)realloc(s, n + 10);
42*89a1d03eSRichard }
43*89a1d03eSRichard 
bad_calloc(int n,int m)44*89a1d03eSRichard void bad_calloc(int n, int m) {
45*89a1d03eSRichard   char *p = (char *)calloc(m, n) + 10;
46*89a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: arithmetic operation is applied to the result of calloc() instead of its size-like argument
47*89a1d03eSRichard   // CHECK-FIXES: char *p = (char *)calloc(m, n + 10);
48*89a1d03eSRichard }
49*89a1d03eSRichard 
50*89a1d03eSRichard void (*(*const alloc_ptr)(size_t)) = malloc;
51*89a1d03eSRichard 
bad_indirect_alloc(int n)52*89a1d03eSRichard void bad_indirect_alloc(int n) {
53*89a1d03eSRichard   char *p = (char *)alloc_ptr(n) + 10;
54*89a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: arithmetic operation is applied to the result of alloc_ptr() instead of its size-like argument
55*89a1d03eSRichard   // CHECK-FIXES: char *p = (char *)alloc_ptr(n + 10);
56*89a1d03eSRichard }
57