-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathtest.c
94 lines (77 loc) · 2.96 KB
/
test.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
#include <stdio.h>
#include <wchar.h>
void sample1() {
/* Test for plain char type */
const char *s1 =
"string1"; // COMPLIANT: string literal assigned to a const char* variable
const register volatile char *s2 =
"string2"; // COMPLIANT: string literal assigned to a const char*
// variable, don't care about the qualifiers
char *s3 = "string3"; // NON_COMPLIANT: char* variable declared to hold a
// string literal
s3 =
"string4"; // NON_COMPLIANT: char* variable assigned a string literal
// (not likely to be seen in production, since there is strcpy)
/* Test for wide char type */
const wchar_t *ws1 = L"wide string1"; // COMPLIANT: string literal assigned to
// a const char* variable
const register volatile wchar_t *ws2 =
L"wide string2"; // COMPLIANT: string literal assigned to a const char*
// variable, don't care about the qualifiers
wchar_t *ws3 = L"wide string3"; // NON_COMPLIANT: char* variable declared to
// hold a string literal
ws3 = L"wide string4"; // NON_COMPLIANT: char* variable assigned a string
// literal (not likely to be seen in production, since
// there is strcpy)
}
/* Testing returning a plain string literal */
const char *sample2(int x) {
if (x == 1)
return "string5"; // COMPLIANT: can return a string literal with return type
// being const char* being const char*
else
return NULL;
}
/* Testing returning a wide string literal */
const wchar_t *w_sample2(int x) {
if (x == 1)
return L"string5"; // COMPLIANT: can return a string literal with return
// type being const char* being const char*
else
return NULL;
}
char *sample3(int x) {
if (x == 1)
return "string6"; // NON_COMPLIANT: can return a string literal with return
// type being char*
else
return NULL;
}
wchar_t *w_sample3(int x) {
if (x == 1)
return L"string6"; // NON_COMPLIANT: can return a string literal with return
// type being char*
else
return NULL;
}
void sample4(char *string) {}
void sample5(const char *string) {}
void call45() {
sample4("string8"); // NON_COMPLIANT: can't pass string literal to char*
sample5("string9"); // COMPLIANT: passing string literal to const char*
}
void w_sample4(wchar_t *string) {}
void w_sample5(const wchar_t *string) {}
void w_call45() {
w_sample4(L"string8"); // NON_COMPLIANT: can't pass string literal to char*
w_sample5(L"string9"); // COMPLIANT: passing string literal to const char*
}
void w_sample6(int x, ...) {}
void w_call6() {
w_sample6(1, "string10"); // COMPLIANT by first (and only) exception
}
void w_sample7(char *x, ...) {}
void w_call7() {
w_sample7("string11", 1); // NON_COMPLIANT, does not fit exceptional case
}
int main() { return 0; }