| 1 |
/* { dg-additional-options "-O0 -fno-exceptions -fno-analyzer-suppress-followups" } */ |
| 2 |
#include <new> |
| 3 |
|
| 4 |
/* Test non-throwing variants of operator new */ |
| 5 |
|
| 6 |
struct A |
| 7 |
{ |
| 8 |
int x; |
| 9 |
int y; |
| 10 |
}; |
| 11 |
|
| 12 |
void test_throwing () |
| 13 |
{ |
| 14 |
int* x = new int; |
| 15 |
int* y = new int(); /* { dg-warning "dereference of possibly-NULL" } */ |
| 16 |
int* arr = new int[10]; |
| 17 |
A *a = new A(); /* { dg-warning "dereference of possibly-NULL" } */ |
| 18 |
|
| 19 |
int z = *y + 2; |
| 20 |
z = *x + 4; /* { dg-warning "dereference of possibly-NULL 'x'" } */ |
| 21 |
/* { dg-warning "use of uninitialized value '\\*x'" "" { target *-*-* } .-1 } */ |
| 22 |
z = arr[0] + 4; /* { dg-warning "dereference of possibly-NULL 'arr'" } */ |
| 23 |
/* { dg-warning "use of uninitialized value '\\*arr'" "" { target *-*-* } .-1 } */ |
| 24 |
a->y = a->x + 3; |
| 25 |
|
| 26 |
delete a; |
| 27 |
delete y; |
| 28 |
delete x; |
| 29 |
delete[] arr; |
| 30 |
} |
| 31 |
|
| 32 |
void test_nonthrowing () |
| 33 |
{ |
| 34 |
int* x = new(std::nothrow) int; |
| 35 |
int* y = new(std::nothrow) int(); |
| 36 |
int* arr = new(std::nothrow) int[10]; |
| 37 |
|
| 38 |
int z = *y + 2; /* { dg-warning "dereference of NULL 'y'" } */ |
| 39 |
/* { dg-bogus "use of uninitialized value '\\*y'" "" { target *-*-* } .-1 } */ |
| 40 |
z = *x + 4; /* { dg-warning "dereference of possibly-NULL 'x'" } */ |
| 41 |
/* { dg-warning "use of uninitialized value '\\*x'" "" { target *-*-* } .-1 } */ |
| 42 |
z = arr[0] + 4; /* { dg-warning "dereference of possibly-NULL 'arr'" } */ |
| 43 |
/* { dg-warning "use of uninitialized value '\\*arr'" "" { target *-*-* } .-1 } */ |
| 44 |
|
| 45 |
delete y; |
| 46 |
delete x; |
| 47 |
delete[] arr; |
| 48 |
} |