Skip to content

Commit 25286ad

Browse files
committed
Add test.c for RULE-7-4
1 parent 3034493 commit 25286ad

File tree

1 file changed

+55
-0
lines changed
  • c/misra/test/rules/RULE-7-4

1 file changed

+55
-0
lines changed

c/misra/test/rules/RULE-7-4/test.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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; }

0 commit comments

Comments
 (0)