1 /* { dg-skip-if "requires hosted libstdc++ for stdlib malloc" { ! hostedlib } } */
7 /* Tests with constant buffer sizes. */
11 int16_t *ptr
= (int16_t *) malloc (21 * sizeof (int16_t));
17 int32_t *ptr
= (int32_t *) malloc (21 * sizeof (int16_t)); /* { dg-line malloc2 } */
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 } */
28 void *ptr
= malloc (21 * sizeof (int16_t));
29 int16_t *sptr
= (int16_t *)ptr
;
35 void *ptr
= malloc (21 * sizeof (int16_t)); /* { dg-message "42 bytes" } */
36 int32_t *iptr
= (int32_t *)ptr
; /* { dg-line assign4 } */
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 } */
47 scanf("%i", &user_input
);
50 n
= 21 * sizeof (int16_t);
52 n
= 42 * sizeof (int16_t);
53 void *ptr
= malloc (n
);
54 int16_t *sptr
= (int16_t *)ptr
;
61 scanf("%i", &user_input
);
64 n
= 21 * sizeof (int16_t);
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 } */
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 } */
81 scanf("%i", &user_input
);
85 else if (user_input
== 2)
89 /* n is an unknown_svalue at this point. */
90 void *ptr
= malloc (n
);
91 int32_t *iptr
= (int32_t *)ptr
;
95 void *create_buffer (int32_t n
)
102 int32_t *buf
= (int32_t *) create_buffer(4 * sizeof (int));
108 int32_t *buf
= (int32_t *) create_buffer(42); /* { dg-warning "allocated buffer size is not a multiple of the pointee's size" } */
112 void test_10 (int32_t n
)
114 char *ptr
= (char *) malloc (7 * n
);
120 /* 3.0 is folded to an int before the analyzer runs. */
121 int32_t *ptr
= (int32_t *) malloc (3.0); /* { dg-line malloc11 } */
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 } */