-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtconst.c
252 lines (204 loc) · 5.42 KB
/
tconst.c
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
/*
* cdecl:
*
* const char * const - a const pointer to const char
*
* const char ** - a pointer to pointer to a const char (char values in read-only)
*
* char * const * - a pointer to const pointer to a char
*
* char ** const - a const pointer to pointer to a char
*
* const char ** const - a const pointer to pointer to a const char
*
* const char * const * const - a const pointer to a const pointer to a const char
*/
char * foo(char *);
const char * foo_1(const char *);
char * foo_2(const char *);
void foo_3(const char *, char const **);
void foo_4(const char *, char **);
char * foo_5(const char *);
char * foo_6(const char *);
char *foo_7(const char *, char * const []);
char *foo_8(const char *, const char * const []);
char *foo_9(const char *, char * const *);
char *foo_10(const char *, const char * const *);
const char *foo_11(const char *);
char *
foo(char *bar)
{
#if 0
bar[4] = '*'; /* can cause UB if was passed a string constant */
#endif
return bar;
}
const char *
foo_1(const char *bar)
{
return bar;
}
char *
foo_2(const char *bar)
{
return bar; /* return discards const */
}
void
foo_3(const char *bar, const char **none)
{
*none = bar;
return;
}
void
foo_4(const char *bar, char **none)
{
*none = bar; /* assignment discards const */
return;
}
char *
foo_5(const char *bar)
{
return *((char **) &bar); /* cast discards const */
}
char *
foo_6(const char *bar)
{
/* unconst */
return (void *) (uintptr_t) (const void *) bar; /* OK!!! */
}
char *
foo_7(const char *bar, char * const none[])
{
bar = none[0];
printf("%s\n", bar);
return none[0];
}
char *
foo_8(const char *bar, const char * const none[])
{
bar = none[0];
printf("%s\n", bar);
return none[0]; /* discards qualifiers from pointer target type */
}
char *
foo_9(const char *bar, char * const *none)
{
bar = *none;
printf("%s\n", bar);
return none[0];
}
char *
foo_10(const char *bar, const char * const *none)
{
bar = *none;
printf("%s\n", bar);
return none[0]; /* discards qualifiers from pointer target type */
}
const char *
foo_11(const char *bar)
{
bar = malloc((size_t) 100); /* XXX XXX XXX NO ERROR OR WARNING!!! XXX XXX XXX */
return bar; /* XXX still no error, though here it is understandable */
}
int main(int, char *[]);
int
main(int argc __unused,
char *argv[])
{
char blah[100];
const char cblah[100];
const char *cbork = argv[0];
char *bork = NULL;
char * const borkc = NULL;
char fooarray[] = "abc"; /* n.b.: no warning, even with
* -Wwrite-strings, because it's just an
* initializer for a writable array
*
* N.B.B.: This is the correct (but not
* necessarily most efficient) way to
* get a reference to a string
* "constant" that can be passed to a
* non-const-qualified parameter.
*/
printf("%s\n", fooarray);
bork = foo("a literal string constant\n"); /* xxx only with -Wwrite-strings: discards qualifiers from pointer target type */
printf(bork); /* format is not a string constant */
cbork = foo_1("this is a literal too\n");
bork = foo((const char *) "a literal string constant\n"); /* discards qualifiers from pointer target type */
cbork = foo_1((const char *) "this is a literal too\n");
cbork = blah;
bork = cbork; /* assignment discards const */
*bork = '\0';
cbork = cblah;
bork = cbork; /* assignment discards const */
*bork = '\0';
cbork = blah;
/* unconst */
bork = (void *) (uintptr_t) (const void *) cbork; /* OK */
*bork = '\0';
cbork = cblah;
/* unconst */
bork = (void *) (uintptr_t) (const void *) cbork; /* OK */
*bork = '\0';
cbork = foo_1(cblah);
bork = foo_1(blah); /* assignment discards const */
*bork = '\0';
bork = foo_2(blah);
*bork = '\0';
bork = foo_2(cblah);
*bork = '\0';
foo_3(blah, &cbork);
*bork = '\0';
foo_3(blah, &bork); /* incompatible pointer type */
*bork = '\0';
foo_3(blah, &borkc); /* incompatible pointer type */
*bork = '\0';
foo_3(blah, (const char **) &bork); /* xxx clang only: must have all intermediate pointers const qualified to be safe */
*bork = '\0';
foo_3(cbork, (char const **) &bork); /* silly form */ /* xxx clang only: must have all intermediate pointers const qualified to be safe */
*bork = '\0';
foo_3(bork, (char ** const) &bork); /* incompatible pointer type */
*bork = '\0';
foo_3(bork, (char * const *) &bork); /* incompatible pointer type */
*bork = '\0';
foo_3(bork, (char * const * const) &bork); /* incompatible pointer type */
*bork = '\0';
foo_3(bork, (const char * const * const) &bork);/* discards qualifiers from pointer target type */
*bork = '\0';
foo_4(blah, &bork);
*bork = '\0';
bork = foo_5(blah);
*bork = '\0';
bork = foo_5(cblah);
*bork = '\0';
bork = foo_5(cbork);
*bork = '\0';
bork = foo_5(bork);
*bork = '\0';
bork = foo_6(cblah);
*bork = '\0';
bork = foo_6(blah);
*bork = '\0';
bork = foo_6(cbork);
*bork = '\0';
bork = foo_6(bork);
*bork = '\0';
foo_7(blah, argv);
foo_8(blah, argv); /* incompatible pointer type */
foo_9(blah, argv);
foo_10(blah, argv); /* incompatible pointer type */
cbork = foo_11(blah);
exit(0);
/* NOTREACHED */
}
/*
* if not in CFLAGS already: CPPFLAGS='-Wwrite-strings
*
* Local Variables:
* eval: (make-local-variable 'compile-command)
* compile-command: (let ((fn (file-name-sans-extension (file-name-nondirectory (buffer-file-name))))) (concat "rm -f " fn "; " (default-value 'compile-command) " " fn " && ./" fn))
* End:
*/