Skip to content

Commit 14350d0

Browse files
committed
modified test.c for 7-4
1 parent 25286ad commit 14350d0

File tree

1 file changed

+24
-11
lines changed
  • c/misra/test/rules/RULE-7-4

1 file changed

+24
-11
lines changed

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ void sample1() {
66
const register volatile char *s2 =
77
"string"; // COMPLIANT: string literal assigned to a const char* variable,
88
// 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
1315
}
1416

1517
const char *sample2(int x) {
@@ -28,7 +30,7 @@ char *sample3(int x) {
2830
return NULL;
2931
}
3032

31-
const char *sample6(int x) {
33+
const char *sample4(int x) {
3234
const char *result;
3335
if (x == 1)
3436
result = "string"; // COMPLIANT: string literal assigned to a const char*
@@ -40,16 +42,27 @@ const char *sample6(int x) {
4042
// being const char*
4143
}
4244

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) {}
4457

45-
void sample5(const char *string) {}
58+
void sample7(const char *string) {}
4659

4760
void call45() {
4861
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*
5366
}
5467

5568
int main() { return 0; }

0 commit comments

Comments
 (0)