Debian Codegraph @

gcc/testsuite/gcc.dg/uninit-41.c

raw
1 /* Verify that calls to non-modifying built-ins aren't considered
2 potentially modifying.
3 { dg-do compile }
4 { dg-options "-std=gnu17 -O2 -Wall" } */
5
6 typedef __SIZE_TYPE__ size_t;
7
8 void* alloca (size_t);
9 void* calloc (size_t, size_t);
10 void* malloc (size_t);
11 int printf (const char *, ...);
12 int scanf (const char *, ...);
13 int sprintf (char *, const char *, ...);
14 int snprintf (char *, size_t, const char *, ...);
15 int puts (const char *);
16 char* strcpy (char*, const char*);
17 size_t strlen (const char*);
18
19 void noproto ();
20
21 void sink (int, ...);
22
23 extern char a[];
24
25 void nowarn_noproto (const char *fmt)
26 {
27 int i;
28 noproto (&i);
29 sink (i);
30 }
31
32 void nowarn_scanf (const char *fmt)
33 {
34 int i;
35 scanf ("%i", &i);
36 sink (i);
37 }
38
39 void test_puts_sprintf_alloca (const char *fmt)
40 {
41 char *p;
42 {
43 p = alloca (8);
44 sprintf (a, fmt, p); // fmt might contain %n
45 puts (p);
46 }
47
48 {
49 p = alloca (8);
50 snprintf (0, 0, fmt, p); // same as above
51 puts (p);
52 }
53 }
54
55 void test_puts_alloca (const char *s)
56 {
57 char *p = alloca (8);
58
59 {
60 char a[] = "foo";
61 puts (a);
62 }
63
64 puts (p); // { dg-warning "-Wuninitialized" }
65
66 {
67 p = alloca (strlen (s) + 1);
68 strcpy (p, s);
69 puts (p);
70 }
71
72 {
73 /* Verify that the puts() calls above isn't considered to have
74 potentially modified *P, and same for the one below. */
75 p = alloca (strlen (s));
76 puts (p); // { dg-warning "-Wuninitialized" }
77 puts (p + 1); // { dg-warning "-Wuninitialized" }
78 }
79 }
80
81
82 void test_puts_malloc (const char *s, const char *t)
83 {
84 char *p;
85
86 {
87 p = malloc (strlen (s) + 1);
88 strcpy (p, s);
89 puts (p);
90 }
91
92 {
93 p = malloc (strlen (t));
94 puts (p); // { dg-warning "-Wuninitialized" }
95 }
96 }
97
98
99 void test_puts_vla (const char *s, const char *t)
100 {
101 {
102 char a[strlen (s) + 1];
103 strcpy (a, s);
104 puts (a);
105 }
106
107 {
108 char b[strlen (t)];
109 puts (b); // { dg-warning "-Wuninitialized" }
110 }
111 }
112
113
114 void test_printf_puts (const char *s)
115 {
116 char *p = __builtin_malloc (1);
117
118 printf ("%s", s);
119
120 puts (p); // { dg-warning "-Wuninitialized" }
121 }