Debian Codegraph @

gcc/testsuite/c-c++-common/analyzer/allocation-size-3.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 /* { dg-additional-options -Wno-analyzer-out-of-bounds } */
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <stdint.h>
10
11 /* CWE-131 example 5 */
12 void test_1 (void)
13 {
14 int32_t *id_sequence = (int32_t *) malloc (3); /* { dg-line malloc1 } */
15 if (id_sequence == NULL) exit (1);
16
17 id_sequence[0] = 13579;
18 id_sequence[1] = 24680;
19 id_sequence[2] = 97531;
20
21 free (id_sequence);
22
23 /* { dg-warning "allocated buffer size is not a multiple of the pointee's size \\\[CWE-131\\\]" "warning" { target *-*-* } malloc1 } */
24 /* { dg-message "3 bytes" "note" { target *-*-* } malloc1 } */
25 /* { dg-message "'int32_t \\*' (\\\{aka '(long )?int \\*'\\\})? here; 'sizeof \\(int32_t (\\\{aka (long )?int\\\})?\\)' is '4'" "note" { target c } malloc1 } */
26 /* { dg-message "'int32_t\\*' (\\\{aka '(long )?int\\*'\\\})? here; 'sizeof \\(int32_t (\\\{aka (long )?int\\\})?\\)' is '4'" "note" { target c++ } malloc1 } */
27 }
28
29 void test_2 (void)
30 {
31 int32_t *ptr = (int32_t *) malloc (10 + sizeof(int32_t)); /* { dg-line malloc2 } */
32 free (ptr);
33
34 /* { dg-warning "allocated buffer size is not a multiple of the pointee's size \\\[CWE-131\\\]" "warning" { target *-*-* } malloc2 } */
35 /* { dg-message "14 bytes" "note" { target *-*-* } malloc2 } */
36 /* { dg-message "'int32_t \\*' (\\\{aka '(long )?int \\*'\\\})? here; 'sizeof \\(int32_t (\\\{aka (long )?int\\\})?\\)' is '4'" "note" { target c } malloc2 } */
37 /* { dg-message "'int32_t\\*' (\\\{aka '(long )?int\\*'\\\})? here; 'sizeof \\(int32_t (\\\{aka (long )?int\\\})?\\)' is '4'" "note" { target c++ } malloc2 } */
38 }
39
40 void test_3 (int32_t n)
41 {
42 int32_t *ptr = (int32_t *) malloc (n + sizeof (int32_t)); /* { dg-line malloc3 } */
43 free (ptr);
44
45 /* { dg-warning "allocated buffer size is not a multiple of the pointee's size \\\[CWE-131\\\]" "warning" { target *-*-* } malloc3 } */
46 /* { dg-message "'\[a-z0-9\\+\\(\\)\\s\]*' bytes" "note" { target *-*-* } malloc3 } */
47 /* { dg-message "'int32_t \\*' (\\\{aka '(long )?int \\*'\\\})? here; 'sizeof \\(int32_t (\\\{aka (long )?int\\\})?\\)' is '4'" "note" { target c } malloc3 } */
48 /* { dg-message "'int32_t\\*' (\\\{aka '(long )?int\\*'\\\})? here; 'sizeof \\(int32_t (\\\{aka (long )?int\\\})?\\)' is '4'" "note" { target c++ } malloc3 } */
49 }
50
51 void test_4 (int32_t n, int32_t m)
52 {
53 int32_t *ptr = (int32_t *) malloc ((n + m) * sizeof (int32_t));
54 free (ptr);
55 }