| 1 |
/* { dg-additional-options "-Wno-placement-new -Wno-analyzer-use-of-uninitialized-value" } */ |
| 2 |
/* { dg-skip-if "requires hosted libstdc++ for stdlib malloc" { ! hostedlib } } */ |
| 3 |
|
| 4 |
#include <new> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <stdint.h> |
| 7 |
|
| 8 |
extern int get_buf_size (); |
| 9 |
|
| 10 |
void var_too_short () |
| 11 |
{ |
| 12 |
int8_t s; |
| 13 |
int64_t *lp = new (&s) int64_t; /* { dg-warning "stack-based buffer overflow" } */ |
| 14 |
/* { dg-warning "allocated buffer size is not a multiple of the pointee's size" "" { target *-*-* } .-1 } */ |
| 15 |
} |
| 16 |
|
| 17 |
void static_buffer_too_short () |
| 18 |
{ |
| 19 |
int n = 16; |
| 20 |
int buf[n]; |
| 21 |
int *p = new (buf) int[n + 1]; /* { dg-warning "stack-based buffer overflow" } */ |
| 22 |
} |
| 23 |
|
| 24 |
void symbolic_buffer_too_short () |
| 25 |
{ |
| 26 |
int n = get_buf_size (); |
| 27 |
char buf[n]; |
| 28 |
char *p = new (buf) char[n + 10]; /* { dg-warning "stack-based buffer overflow" } */ |
| 29 |
} |
| 30 |
|
| 31 |
void test_binop () |
| 32 |
{ |
| 33 |
char *p = (char *) malloc (4); |
| 34 |
if (!p) |
| 35 |
return; |
| 36 |
int32_t *i = ::new (p + 1) int32_t; /* { dg-warning "heap-based buffer overflow" } */ |
| 37 |
*i = 42; /* { dg-warning "heap-based buffer overflow" } */ |
| 38 |
free (p); |
| 39 |
} |