Debian Codegraph @

gcc/testsuite/c-c++-common/analyzer/allocation-size-2.c

raw
1 /* { dg-additional-options "-fanalyzer-fine-grained" }
2 { dg-skip-if "requires hosted libstdc++ for stdlib malloc" { ! hostedlib } }
3 -fanalyzer-fine-grained is currently required; see PR analyzer/107851. */
4
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <stdint.h>
8
9 /* Tests with symbolic buffer sizes. */
10
11 void test_1 (int32_t n)
12 {
13 int16_t *ptr = (int16_t *)malloc (n * sizeof (int16_t));
14 free (ptr);
15 }
16
17 void test_2 (int32_t n)
18 {
19 int32_t *ptr = (int32_t *)malloc (n * sizeof (int16_t)); /* { dg-line malloc2 } */
20 free (ptr);
21
22 /* { dg-warning "allocated buffer size is not a multiple of the pointee's size \\\[CWE-131\\\]" "warning" { target *-*-* } malloc2 } */
23 /* { dg-message "'\[a-z0-9\\*\\(\\)\\s\]*' bytes" "note" { target *-*-* } malloc2 } */
24 /* { dg-message "'int32_t \\*' (\\\{aka '(long )?int \\*'\\\})? here; 'sizeof \\(int32_t (\\\{aka (long )?int\\\})?\\)' is '4" "note" { target c } malloc2 } */
25 /* { dg-message "'int32_t\\*' (\\\{aka '(long )?int\\*'\\\})? here; 'sizeof \\(int32_t (\\\{aka (long )?int\\\})?\\)' is '4" "note" { target c++ } malloc2 } */
26 }
27
28 void test_3 (int32_t n)
29 {
30 void *ptr = malloc (n * sizeof (int16_t));
31 int16_t *sptr = (int16_t *)ptr;
32 free (sptr);
33 }
34
35 void test_4 (int32_t n)
36 {
37 void *ptr = malloc (n * sizeof (int16_t)); /* { dg-message "'\[a-z0-9\\*\\(\\)\\s\]*'" "note" } */
38 int32_t *iptr = (int32_t *)ptr; /* { dg-line assign4 } */
39 free (iptr);
40
41 /* { dg-warning "allocated buffer size is not a multiple of the pointee's size \\\[CWE-131\\\]" "warning" { target *-*-* } assign4 } */
42 /* { dg-message "'int32_t \\*' (\\\{aka '(long )?int \\*'\\\})? here; 'sizeof \\(int32_t (\\\{aka (long )?int\\\})?\\)' is '4'" "note" { target c } assign4 } */
43 /* { dg-message "'int32_t\\*' (\\\{aka '(long )?int\\*'\\\})? here; 'sizeof \\(int32_t (\\\{aka (long )?int\\\})?\\)' is '4'" "note" { target c++ } assign4 } */
44 }
45
46 void test_5 (void)
47 {
48 int32_t user_input;
49 scanf("%i", &user_input);
50 int32_t n;
51 if (user_input == 0)
52 n = 3 * user_input * sizeof (int16_t);
53 else
54 n = 5 * user_input * sizeof (int16_t);
55 void *ptr = malloc (n);
56 int16_t *sptr = (int16_t *)ptr;
57 free (sptr);
58 }
59
60 void test_6 (void)
61 {
62 int32_t user_input;
63 scanf("%i", &user_input);
64 int32_t n;
65 if (user_input == 0)
66 n = user_input;
67 else if (user_input == 2)
68 n = user_input * 3;
69 else
70 n = user_input * 5;
71 /* n is an unknown_svalue at this point. */
72 void *ptr = malloc (n);
73 int32_t *iptr = (int32_t *)ptr;
74 free (iptr);
75 }
76
77 void *create_buffer(int32_t n)
78 {
79 return malloc(n);
80 }
81
82 void test_7(int32_t n)
83 {
84 int32_t *buf = (int32_t *)create_buffer(n * sizeof (int32_t));
85 free (buf);
86 }
87
88 void test_8(int32_t n)
89 {
90 int32_t *buf = (int32_t *)create_buffer(n * sizeof(int16_t)); /* { dg-warning "allocated buffer size is not a multiple of the pointee's size" } */
91 free (buf);
92 }
93
94 void test_9 (void)
95 {
96 int32_t n;
97 scanf("%i", &n);
98 /* n is a conjured_svalue without any constraint. We have to assume
99 that is a multiple of sizeof (int32_t *); see PR analyzer/110014. */
100 void *ptr = malloc (n);
101 int32_t *iptr = (int32_t *)ptr;
102 free (iptr);
103 }
104
105 void test_11 (void)
106 {
107 int32_t n;
108 scanf("%i", &n);
109 void *ptr = malloc (n);
110 if (n == sizeof (int32_t))
111 {
112 /* n is a conjured_svalue but guarded such that we
113 know the value is a multiple of sizeof (*iptr). */
114 int32_t *iptr = (int32_t *)ptr;
115 free (iptr);
116 }
117 else
118 free (ptr);
119 }
120
121 void test_12 (void)
122 {
123 int32_t n;
124 scanf("%i", &n);
125 void *ptr = malloc (n); /* { dg-message "'n' bytes" } */
126 if (n == 5)
127 {
128 /* n is a conjured_svalue but guarded such that we
129 know the value isn't a multiple of sizeof (*iptr). */
130 int32_t *iptr = (int32_t *)ptr; /* { dg-line assign12 } */
131 free (iptr);
132 }
133 else
134 free (ptr);
135 /* { dg-warning "allocated buffer size is not a multiple of the pointee's size \\\[CWE-131\\\]" "warning" { target *-*-* } assign12 } */
136 /* { dg-message "'int32_t \\*' (\\\{aka '(long )?int \\*'\\\})? here; 'sizeof \\(int32_t (\\\{aka (long )?int\\\})?\\)' is '4'" "note" { target c } assign12 } */
137 /* { dg-message "'int32_t\\*' (\\\{aka '(long )?int\\*'\\\})? here; 'sizeof \\(int32_t (\\\{aka (long )?int\\\})?\\)' is '4'" "note" { target c++ } assign12 } */
138 }
139
140 void test_13 (void)
141 {
142 int32_t n;
143 scanf("%i", &n);
144 void *ptr = malloc (n);
145 if (n == n * n)
146 {
147 /* n is a conjured_svalue but guarded such that we don't have an
148 equivalence class for it. In such cases, we assume that the
149 condition ensures that the value is okay. */
150 int32_t *iptr = (int32_t *)ptr;
151 free (iptr);
152 }
153 else
154 free (ptr);
155 }
156
157 int *test_14 (size_t n)
158 {
159 int *ptr = NULL;
160 /* n is an initial_svalue and guarded such that there is no equiv_class
161 for n itself but only for a binop_svalue containing n. */
162 if (n % sizeof (int) == 0)
163 ptr = (int *)malloc (n);
164 return ptr;
165 }