aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/Wdangling-pointer.c
blob: 89e222038dbc9c4f90e7bb0d4f0b71680e476bd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* Exercise basic C-only cases of -Wdangling-pointer.
   { dg-do compile }
   { dg-options "-O0 -Wall" }
   { dg-require-effective-target alloca } */

typedef __SIZE_TYPE__ size_t;

extern void* memchr (const void*, int, size_t);
extern char* strchr (const char*, int);

void sink (const void*, ...);


void nowarn_compound_literal (int i)
{
  {
    int *p = (int[]){ 1, 2, 3 };
    sink (p);
  }
  {
    int *q = (int[]){ 1, 2, 3 };
    int *p = &q[1];
    sink (p);
  }
  {
    int *p = __builtin_memchr ((int[]){ 1, 2, 3 }, 2, 3 * sizeof *p);
    sink (p);
  }
  {
    int *p = __builtin_memchr ((int[]){ i, i + 1 }, 3, 2 * sizeof *p);
    sink (p);
  }
}


void warn_compound_literal (int i)
{
  int *p;
  {
    p = (int[]){ 1, 2, 3 };   // { dg-message "unnamed temporary" },
  }
  sink (p);                   // { dg-warning "using dangling pointer 'p' to an unnamed temporary" }

  {
    int *q =
      (int[]){ 1, 2, 3 };     // { dg-message "unnamed temporary" },
    p = &q[1];
  }
  sink (p);                   // { dg-warning "using dangling pointer 'p' to an unnamed temporary" }
  {
    p = (int*)memchr (
	  (int[]){ 1, 2, 3 }, // { dg-message "unnamed temporary" }
	  2, 3 * sizeof *p);
  }
  sink (p);                   // { dg-warning "using dangling pointer 'p' to an unnamed temporary" }

  {
    p = (int*)memchr (
	  (int[]){ i, i + 1 },// { dg-message "unnamed temporary" }
	  3, 2 * sizeof *p);
  }
  sink (p);                   // { dg-warning "using dangling pointer 'p' to an unnamed temporary" }
}


void warn_store_compound_literal (int **p)
{
  int *q = (int[]) { 1, 2, 3 };
  p[0] = q;                   // { dg-warning "storing the address" }
}

void warn_store_vla (int n, int **p)
{
  int a[n];
  p[1] = &a[1];               // { dg-warning "-Wdangling-pointer" "pr??????" { xfail *-*-* } }
}