]>
gcc.gnu.org Git - gcc.git/blob - gcc/testsuite/c-c++-common/analyzer/malloc-ipa-8-unchecked.c
1 /* Example of a multilevel wrapper around malloc, with an unchecked write. */
3 /* { dg-additional-options "-fdiagnostics-show-line-numbers -fdiagnostics-path-format=inline-events -fanalyzer-checker=malloc -fdiagnostics-show-caret" } */
4 /* { dg-enable-nn-line-numbers "" } */
8 void *wrapped_malloc (size_t size
)
13 typedef struct boxed_int
19 make_boxed_int (int i
)
21 boxed_int
*result
= (boxed_int
*)wrapped_malloc (sizeof (boxed_int
));
22 result
->i
= i
; /* { dg-warning "dereference of possibly-NULL 'result'" } */
26 /* "dereference of possibly-NULL 'result' [CWE-690]". */
27 /* { dg-begin-multiline-output "" }
30 'make_boxed_int': events 1-2
32 | NN | make_boxed_int (int i)
35 | | (1) entry to 'make_boxed_int'
37 | NN | boxed_int *result = (boxed_int *)wrapped_malloc (sizeof (boxed_int));
38 | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
40 | | (2) calling 'wrapped_malloc' from 'make_boxed_int'
42 +--> 'wrapped_malloc': events 3-4
44 | NN | void *wrapped_malloc (size_t size)
47 | | (3) entry to 'wrapped_malloc'
49 | NN | return malloc (size);
52 | | (4) this call could return NULL
56 'make_boxed_int': events 5-6
58 | NN | boxed_int *result = (boxed_int *)wrapped_malloc (sizeof (boxed_int));
59 | | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
61 | | (5) possible return of NULL to 'make_boxed_int' from 'wrapped_malloc'
65 | | (6) 'result' could be NULL: unchecked value from (4)
67 { dg-end-multiline-output "" { target c } } */
68 /* { dg-begin-multiline-output "" }
71 'boxed_int* make_boxed_int(int)': events 1-2
73 | NN | make_boxed_int (int i)
76 | | (1) entry to 'make_boxed_int'
78 | NN | boxed_int *result = (boxed_int *)wrapped_malloc (sizeof (boxed_int));
79 | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
81 | | (2) calling 'wrapped_malloc' from 'make_boxed_int'
83 +--> 'void* wrapped_malloc(size_t)': events 3-4
85 | NN | void *wrapped_malloc (size_t size)
88 | | (3) entry to 'wrapped_malloc'
90 | NN | return malloc (size);
93 | | (4) this call could return NULL
97 'boxed_int* make_boxed_int(int)': events 5-6
99 | NN | boxed_int *result = (boxed_int *)wrapped_malloc (sizeof (boxed_int));
100 | | ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
102 | | (5) possible return of NULL to 'make_boxed_int' from 'wrapped_malloc'
103 | NN | result->i = i;
106 | | (6) 'result' could be NULL: unchecked value from (4)
108 { dg-end-multiline-output "" { target c++ } } */
This page took 0.042017 seconds and 5 git commands to generate.