diff options
Diffstat (limited to 'gcc/testsuite/gcc.dg/analyzer/malloc-paths-8.c')
-rw-r--r-- | gcc/testsuite/gcc.dg/analyzer/malloc-paths-8.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/gcc/testsuite/gcc.dg/analyzer/malloc-paths-8.c b/gcc/testsuite/gcc.dg/analyzer/malloc-paths-8.c new file mode 100644 index 0000000..bf858e0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/malloc-paths-8.c @@ -0,0 +1,54 @@ +/* { dg-additional-options "-fanalyzer-transitivity" } */ + +#include <stddef.h> +#include <stdlib.h> + +extern void do_stuff (const void *); + +#define LIMIT 1024 + +void test_1 (size_t sz) +{ + void *ptr; + if (sz >= LIMIT) + ptr = malloc (sz); + else + ptr = alloca (sz); + + do_stuff (ptr); + + if (sz >= LIMIT) + free (ptr); +} + +void test_2 (size_t sz) +{ + void *ptr; + if (sz < LIMIT) + ptr = alloca (sz); + else + ptr = malloc (sz); + + do_stuff (ptr); + + if (sz >= LIMIT) + free (ptr); +} + +void test_3 (size_t sz) +{ + void *ptr; + if (sz <= LIMIT) + ptr = alloca (sz); /* { dg-message "memory is allocated on the stack here" } */ + else + ptr = malloc (sz); + + do_stuff (ptr); + + /* Bug: the "sz <= LIMIT" above should have been "sz < LIMIT", + so there's a free-of-alloca when sz == LIMIT. */ + if (sz >= LIMIT) + free (ptr); /* { dg-warning "'free' of memory allocated on the stack by 'alloca'" } */ +} +/* { dg-bogus "leak of 'ptr'" } */ +/* This can't happen, as "sz > 1024" && "sz <= 1023" is impossible. */ |