Add __builtion_unreachable to vector::size(), vector::capacity()
[official-gcc.git] / gcc / testsuite / c-c++-common / analyzer / allocation-size-1.c
bloba9d375d2edc2b67d1c31055ac81b1ddeb8c1414b
1 /* { dg-skip-if "requires hosted libstdc++ for stdlib malloc" { ! hostedlib } } */
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <stdint.h>
7 /* Tests with constant buffer sizes. */
9 void test_1 (void)
11 int16_t *ptr = (int16_t *) malloc (21 * sizeof (int16_t));
12 free (ptr);
15 void test_2 (void)
17 int32_t *ptr = (int32_t *) malloc (21 * sizeof (int16_t)); /* { dg-line malloc2 } */
18 free (ptr);
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 } */
26 void test_3 (void)
28 void *ptr = malloc (21 * sizeof (int16_t));
29 int16_t *sptr = (int16_t *)ptr;
30 free (sptr);
33 void test_4 (void)
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);
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 } */
44 void test_5 (void)
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);
58 void test_6 (void)
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);
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 } */
78 void test_7 (void)
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);
95 void *create_buffer (int32_t n)
97 return malloc(n);
100 void test_8 (void)
102 int32_t *buf = (int32_t *) create_buffer(4 * sizeof (int));
103 free (buf);
106 void test_9 (void)
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);
112 void test_10 (int32_t n)
114 char *ptr = (char *) malloc (7 * n);
115 free (ptr);
118 void test_11 ()
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);
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 } */