Debian Codegraph @

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

raw
1 /* { dg-skip-if "requires hosted libstdc++ for stdlib malloc" { ! hostedlib } } */
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <stdint.h>
6
7 /* Tests with constant buffer sizes. */
8
9 void test_1 (void)
10 {
11 int16_t *ptr = (int16_t *) malloc (21 * sizeof (int16_t));
12 free (ptr);
13 }
14
15 void test_2 (void)
16 {
17 int32_t *ptr = (int32_t *) malloc (21 * sizeof (int16_t)); /* { dg-line malloc2 } */
18 free (ptr);
19
20 /* { dg-warning "allocated buffer size is not a multiple of the pointee's size \\\[CWE-131\\\]" "warning" { target *-*-* } malloc2 } */
21 /* { dg-message "42 bytes" "note" { target *-*-* } malloc2 } */
22 /* { dg-message "'int32_t \\*' (\\\{aka '(long )?int \\*'\\\})? here; 'sizeof \\(int32_t (\\\{aka (long )?int\\\})?\\)' is '4'" "note" { target c } malloc2 } */
23 /* { dg-message "'int32_t\\*' (\\\{aka '(long )?int\\*'\\\})? here; 'sizeof \\(int32_t (\\\{aka (long )?int\\\})?\\)' is '4'" "note" { target c++ } malloc2 } */
24 }
25
26 void test_3 (void)
27 {
28 void *ptr = malloc (21 * sizeof (int16_t));
29 int16_t *sptr = (int16_t *)ptr;
30 free (sptr);
31 }
32
33 void test_4 (void)
34 {
35 void *ptr = malloc (21 * sizeof (int16_t)); /* { dg-message "42 bytes" } */
36 int32_t *iptr = (int32_t *)ptr; /* { dg-line assign4 } */
37 free (iptr);
38
39 /* { dg-warning "allocated buffer size is not a multiple of the pointee's size \\\[CWE-131\\\]" "warning" { target *-*-* } assign4 } */
40 /* { dg-message "'int32_t \\*' (\\\{aka '(long )?int \\*'\\\})? here; 'sizeof \\(int32_t (\\\{aka (long )?int\\\})?\\)' is '4'" "note" { target c } assign4 } */
41 /* { dg-message "'int32_t\\*' (\\\{aka '(long )?int\\*'\\\})? here; 'sizeof \\(int32_t (\\\{aka (long )?int\\\})?\\)' is '4'" "note" { target c++ } assign4 } */
42 }
43
44 void test_5 (void)
45 {
46 int32_t user_input;
47 scanf("%i", &user_input);
48 int32_t n;
49 if (user_input == 0)
50 n = 21 * sizeof (int16_t);
51 else
52 n = 42 * sizeof (int16_t);
53 void *ptr = malloc (n);
54 int16_t *sptr = (int16_t *)ptr;
55 free (sptr);
56 }
57
58 void test_6 (void)
59 {
60 int32_t user_input;
61 scanf("%i", &user_input);
62 int32_t n;
63 if (user_input == 0)
64 n = 21 * sizeof (int16_t);
65 else
66 n = 42 * sizeof (int16_t);
67 void *ptr = malloc (n); /* { dg-message "" "note" } */
68 /* ^^^ on widening_svalues no expr is returned
69 by get_representative_tree at the moment. */
70 int32_t *iptr = (int32_t *)ptr; /* { dg-line assign6 } */
71 free (iptr);
72
73 /* { dg-warning "allocated buffer size is not a multiple of the pointee's size \\\[CWE-131\\\]" "warning" { target *-*-* } assign6 } */
74 /* { dg-message "'int32_t \\*' (\\\{aka '(long )?int \\*'\\\})? here; 'sizeof \\(int32_t (\\\{aka (long )?int\\\})?\\)' is '4'" "note" { target c } assign6 } */
75 /* { dg-message "'int32_t\\*' (\\\{aka '(long )?int\\*'\\\})? here; 'sizeof \\(int32_t (\\\{aka (long )?int\\\})?\\)' is '4'" "note" { target c++ } assign6 } */
76 }
77
78 void test_7 (void)
79 {
80 int32_t user_input;
81 scanf("%i", &user_input);
82 int32_t n;
83 if (user_input == 0)
84 n = 1;
85 else if (user_input == 2)
86 n = 5;
87 else
88 n = 7;
89 /* n is an unknown_svalue at this point. */
90 void *ptr = malloc (n);
91 int32_t *iptr = (int32_t *)ptr;
92 free (iptr);
93 }
94
95 void *create_buffer (int32_t n)
96 {
97 return malloc(n);
98 }
99
100 void test_8 (void)
101 {
102 int32_t *buf = (int32_t *) create_buffer(4 * sizeof (int));
103 free (buf);
104 }
105
106 void test_9 (void)
107 {
108 int32_t *buf = (int32_t *) create_buffer(42); /* { dg-warning "allocated buffer size is not a multiple of the pointee's size" } */
109 free (buf);
110 }
111
112 void test_10 (int32_t n)
113 {
114 char *ptr = (char *) malloc (7 * n);
115 free (ptr);
116 }
117
118 void test_11 ()
119 {
120 /* 3.0 is folded to an int before the analyzer runs. */
121 int32_t *ptr = (int32_t *) malloc (3.0); /* { dg-line malloc11 } */
122 free (ptr);
123
124 /* { dg-warning "allocated buffer size is not a multiple of the pointee's size \\\[CWE-131\\\]" "warning" { target *-*-* } malloc11 } */
125 /* { dg-message "'int32_t \\*' (\\\{aka '(long )?int \\*'\\\})? here; 'sizeof \\(int32_t (\\\{aka (long )?int\\\})?\\)' is '4'" "note" { target c } malloc11 } */
126 /* { dg-message "'int32_t\\*' (\\\{aka '(long )?int\\*'\\\})? here; 'sizeof \\(int32_t (\\\{aka (long )?int\\\})?\\)' is '4'" "note" { target c++ } malloc11 } */
127 }