File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed
c/misra/test/rules/RULE-7-4 Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include <stdio.h>
2
+
3
+ void sample1 () {
4
+ const char * s1 =
5
+ "string" ; // COMPLIANT: string literal assigned to a const char* variable
6
+ const register volatile char * s2 =
7
+ "string" ; // COMPLIANT: string literal assigned to a const char* variable,
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
13
+ }
14
+
15
+ const char * sample2 (int x ) {
16
+ if (x == 1 )
17
+ return "string" ; // COMPLIANT: can return a string literal with return type
18
+ // being const char* being const char*
19
+ else
20
+ return NULL ;
21
+ }
22
+
23
+ char * sample3 (int x ) {
24
+ if (x == 1 )
25
+ return "string" ; // NON_COMPLIANT: can return a string literal with return
26
+ // type being char*
27
+ else
28
+ return NULL ;
29
+ }
30
+
31
+ const char * sample6 (int x ) {
32
+ const char * result ;
33
+ if (x == 1 )
34
+ result = "string" ; // COMPLIANT: string literal assigned to a const char*
35
+ // variable
36
+ else
37
+ result = NULL ;
38
+
39
+ return result ; // COMPLIANT: `result` can be a string literal with return type
40
+ // being const char*
41
+ }
42
+
43
+ void sample4 (char * string ) {}
44
+
45
+ void sample5 (const char * string ) {}
46
+
47
+ void call45 () {
48
+ 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*
53
+ }
54
+
55
+ int main () { return 0 ; }
You can’t perform that action at this time.
0 commit comments