| 1 |
/* Exercise basic C-only cases of -Wdangling-pointer. |
| 2 |
{ dg-do compile } |
| 3 |
{ dg-options "-O0 -Wall" } */ |
| 4 |
|
| 5 |
typedef __SIZE_TYPE__ size_t; |
| 6 |
|
| 7 |
extern void* memchr (const void*, int, size_t); |
| 8 |
extern char* strchr (const char*, int); |
| 9 |
|
| 10 |
void sink (const void*, ...); |
| 11 |
|
| 12 |
|
| 13 |
void nowarn_compound_literal (int i) |
| 14 |
{ |
| 15 |
{ |
| 16 |
int *p = (int[]){ 1, 2, 3 }; |
| 17 |
sink (p); |
| 18 |
} |
| 19 |
{ |
| 20 |
int *q = (int[]){ 1, 2, 3 }; |
| 21 |
int *p = &q[1]; |
| 22 |
sink (p); |
| 23 |
} |
| 24 |
{ |
| 25 |
int *p = __builtin_memchr ((int[]){ 1, 2, 3 }, 2, 3 * sizeof *p); |
| 26 |
sink (p); |
| 27 |
} |
| 28 |
{ |
| 29 |
int *p = __builtin_memchr ((int[]){ i, i + 1 }, 3, 2 * sizeof *p); |
| 30 |
sink (p); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
void warn_compound_literal (int i) |
| 36 |
{ |
| 37 |
int *p; |
| 38 |
{ |
| 39 |
p = (int[]){ 1, 2, 3 }; // { dg-message "unnamed temporary" }, |
| 40 |
} |
| 41 |
sink (p); // { dg-warning "using dangling pointer 'p' to an unnamed temporary" } |
| 42 |
|
| 43 |
{ |
| 44 |
int *q = |
| 45 |
(int[]){ 1, 2, 3 }; // { dg-message "unnamed temporary" }, |
| 46 |
p = &q[1]; |
| 47 |
} |
| 48 |
sink (p); // { dg-warning "using dangling pointer 'p' to an unnamed temporary" } |
| 49 |
{ |
| 50 |
p = (int*)memchr ( |
| 51 |
(int[]){ 1, 2, 3 }, // { dg-message "unnamed temporary" } |
| 52 |
2, 3 * sizeof *p); |
| 53 |
} |
| 54 |
sink (p); // { dg-warning "using dangling pointer 'p' to an unnamed temporary" } |
| 55 |
|
| 56 |
{ |
| 57 |
p = (int*)memchr ( |
| 58 |
(int[]){ i, i + 1 },// { dg-message "unnamed temporary" } |
| 59 |
3, 2 * sizeof *p); |
| 60 |
} |
| 61 |
sink (p); // { dg-warning "using dangling pointer 'p' to an unnamed temporary" } |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
void warn_store_compound_literal (int **p) |
| 66 |
{ |
| 67 |
int *q = (int[]) { 1, 2, 3 }; |
| 68 |
p[0] = q; // { dg-warning "storing the address" } |
| 69 |
} |
| 70 |
|
| 71 |
void warn_store_vla (int n, int **p) |
| 72 |
{ |
| 73 |
int a[n]; |
| 74 |
p[1] = &a[1]; // { dg-warning "-Wdangling-pointer" "pr??????" { xfail *-*-* } } |
| 75 |
} |