aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/uninit-41.c
blob: f2e17a81d0922e94799dc9702a763ff740dceb24 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* Verify that calls to non-modifying built-ins aren't considered
   potentially modifying.
   { dg-do compile }
   { dg-options "-O2 -Wall" }
   { dg-require-effective-target alloca } */

typedef __SIZE_TYPE__ size_t;

void* alloca (size_t);
void* calloc (size_t, size_t);
void* malloc (size_t);
int printf (const char *, ...);
int scanf (const char *, ...);
int sprintf (char *, const char *, ...);
int snprintf (char *, size_t, const char *, ...);
int puts (const char *);
char* strcpy (char*, const char*);
size_t strlen (const char*);

void noproto ();

void sink (int, ...);

extern char a[];

void nowarn_noproto (const char *fmt)
{
  int i;
  noproto (&i);
  sink (i);
}

void nowarn_scanf (const char *fmt)
{
  int i;
  scanf ("%i", &i);
  sink (i);
}

void test_puts_sprintf_alloca (const char *fmt)
{
  char *p;
  {
    p = alloca (8);
    sprintf (a, fmt, p);                // fmt might contain %n
    puts (p);
  }

  {
    p = alloca (8);
    snprintf (0, 0, fmt, p);            // same as above
    puts (p);
  }
}

void test_puts_alloca (const char *s)
{
  char *p = alloca (8);

  {
    char a[] = "foo";
    puts (a);
  }

  puts (p);                             // { dg-warning "-Wuninitialized" }

  {
    p = alloca (strlen (s) + 1);
    strcpy (p, s);
    puts (p);
  }

  {
    /* Verify that the puts() calls above isn't considered to have
       potentially modified *P, and same for the one below.  */
    p = alloca (strlen (s));
    puts (p);                           // { dg-warning "-Wuninitialized" }
    puts (p + 1);                       // { dg-warning "-Wuninitialized" }
  }
}


void test_puts_malloc (const char *s, const char *t)
{
  char *p;

  {
    p = malloc (strlen (s) + 1);
    strcpy (p, s);
    puts (p);
  }

  {
    p = malloc (strlen (t));
    puts (p);                           // { dg-warning "-Wuninitialized" }
  }
}


void test_puts_vla (const char *s, const char *t)
{
  {
    char a[strlen (s) + 1];
    strcpy (a, s);
    puts (a);
  }

  {
    char b[strlen (t)];
    puts (b);                           // { dg-warning "-Wuninitialized" }
  }
}


void test_printf_puts (const char *s)
{
  char *p = __builtin_malloc (1);

  printf ("%s", s);

  puts (p);                             // { dg-warning "-Wuninitialized" }
}