aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/Wdangling-pointer-3.c
blob: 01859303fcfad92057e443b98fda9fa420d1c6a6 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* PR tree-optimization/104715 - false dangling pointer with strstr
   Vertify that using pointers that have become dangling after they were
   passed to and returned from strstr is diagnosed.
  { dg-do compile }
  { dg-options "-Wall" } */

extern char* strstr (const char*, const char*);

void sink (const void*);

void nowarn_strstr_static (const char *s)
{
  char *t1;

  {
    static const char a[] = "abc";
    t1 = strstr (a, s);
    sink (t1);
  }

  sink (t1);
}


void nowarn_strstr_lit (const char *s)
{
  char *t2;

  {
    t2 = strstr ("def", s);
    sink (t2);
  }

  sink (t2);
}


void warn_strstr_comp_lit (const char *s)
{
  char *t3;

  {
    const char *a =
      (char[]){ '1', '\0' };  // { dg-message "unnamed temporary defined here" }
    t3 = strstr (a, s);
    sink (t3);
  }

  sink (t3);            // { dg-warning "using dangling pointer 't3' to an unnamed temporary" }
}


void warn_strstr_arg (const char *s)
{
  char *t4;

  {
    char a[] = "1";     // { dg-message "'a' declared here" }
    t4 = strstr (a, s);
    sink (t4);
  }

  sink (t4);            // { dg-warning "using dangling pointer 't4' to 'a'" }
}


void warn_strstr_arg_plus_cst (const char *s)
{
  char *t5;

  {
    char a[] = "12";    // { dg-message "'a' declared here" }
    t5 = strstr (a + 1, s);
    sink (t5);
  }

  sink (t5);            // { dg-warning "using dangling pointer 't5' to 'a'" }
}


void warn_strstr_arg_plus_var (const char *s, int i)
{
  char *t6;

  {
    char a[] = "123";   // { dg-message "'a' declared here" }
    t6 = strstr (a + i, s);
    sink (t6++);
  }

  sink (t6);            // { dg-warning "using dangling pointer 't6' to 'a'" }
}