@@ -6,10 +6,12 @@ void sample1() {
6
6
const register volatile char * s2 =
7
7
"string" ; // COMPLIANT: string literal assigned to a const char* variable,
8
8
// don't care about the qualifiers
9
- char * s3 =
10
- "string" ; // NON_COMPLIANT: string literal assigned to a char* variable
11
- s2 = s3 ; // COMPLIANT: string literal assigned to a char* variable
12
- s3 = s2 ; // NON_COMPLIANT: string literal assigned to a char* variable
9
+ char * s3 = "string" ; // NON_COMPLIANT: char* variable declared to hold a
10
+ // string literal
11
+ s3 = "string" ; // NON_COMPLIANT: string literal assigned to a char* variable
12
+ s2 = s3 ; // COMPLIANT: string literal (rvalue) assigned to a char* variable
13
+ s3 =
14
+ s2 ; // NON_COMPLIANT: string literal (rvalue) assigned to a char* variable
13
15
}
14
16
15
17
const char * sample2 (int x ) {
@@ -28,7 +30,7 @@ char *sample3(int x) {
28
30
return NULL ;
29
31
}
30
32
31
- const char * sample6 (int x ) {
33
+ const char * sample4 (int x ) {
32
34
const char * result ;
33
35
if (x == 1 )
34
36
result = "string" ; // COMPLIANT: string literal assigned to a const char*
@@ -40,16 +42,27 @@ const char *sample6(int x) {
40
42
// being const char*
41
43
}
42
44
43
- void sample4 (char * string ) {}
45
+ char * sample5 (int x ) {
46
+ const char * result ;
47
+ if (x == 1 )
48
+ result = "string" ; // COMPLIANT: string literal assigned to a const char*
49
+ // variable
50
+ else
51
+ result = NULL ;
52
+
53
+ return result ; // NON_COMPLIANT: `result` can be a string literal with return
54
+ // type being char*
55
+ }
56
+ void sample6 (char * string ) {}
44
57
45
- void sample5 (const char * string ) {}
58
+ void sample7 (const char * string ) {}
46
59
47
60
void call45 () {
48
61
const char * literal = "string" ;
49
- sample4 (literal ); // NON_COMPLIANT: can't pass string literal to char*
50
- sample4 ("string" ); // NON_COMPLIANT: can't pass string literal to char*
51
- sample5 (literal ); // COMPLIANT: passing string literal to const char*
52
- sample5 ("string" ); // COMPLIANT: passing string literal to const char*
62
+ sample6 (literal ); // NON_COMPLIANT: can't pass string literal to char*
63
+ sample6 ("string" ); // NON_COMPLIANT: can't pass string literal to char*
64
+ sample7 (literal ); // COMPLIANT: passing string literal to const char*
65
+ sample7 ("string" ); // COMPLIANT: passing string literal to const char*
53
66
}
54
67
55
68
int main () { return 0 ; }
0 commit comments