Skip to content

Commit 5b64360

Browse files
committed
Factor out test codes
1 parent b9cbb1c commit 5b64360

File tree

10 files changed

+112
-123
lines changed

10 files changed

+112
-123
lines changed

c/misra/test/rules/RULE-19-1/ObjectAssignedToAnOverlappingObject.expected

Lines changed: 0 additions & 1 deletion
This file was deleted.

c/misra/test/rules/RULE-19-1/ObjectAssignedToAnOverlappingObject.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.

c/misra/test/rules/RULE-19-1/ObjectCopiedToAnOverlappingObject.expected

Lines changed: 0 additions & 5 deletions
This file was deleted.

c/misra/test/rules/RULE-19-1/ObjectCopiedToAnOverlappingObject.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.

c/misra/test/rules/RULE-19-1/test.c

Lines changed: 0 additions & 59 deletions
This file was deleted.

cpp/autosar/test/rules/M0-2-1/ObjectAssignedToAnOverlappingObject.expected

Lines changed: 0 additions & 1 deletion
This file was deleted.

cpp/autosar/test/rules/M0-2-1/ObjectAssignedToAnOverlappingObject.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.

cpp/autosar/test/rules/M0-2-1/test.cpp

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
struct s1 {
2+
int m1[10];
3+
};
4+
struct s2 {
5+
int m1;
6+
struct s1 m2;
7+
};
8+
9+
union u {
10+
struct s1 m1;
11+
struct s2 m2;
12+
};
13+
14+
typedef struct {
15+
char buf[8];
16+
} Union_t;
17+
18+
typedef union {
19+
20+
unsigned char uc[24];
21+
22+
struct {
23+
Union_t prefix;
24+
Union_t suffix;
25+
} fnv;
26+
27+
struct {
28+
unsigned char padding[16];
29+
Union_t suffix;
30+
} diff;
31+
32+
} UnionSecret_t;
33+
34+
void overlapping_access() {
35+
u u1;
36+
u1.m2.m2 = u1.m1; // NON_COMPLIANT, different struct. u1.m2 and u1.m1
37+
}
38+
39+
void cross_copy() {
40+
UnionSecret_t hash1;
41+
hash1.diff.suffix =
42+
hash1.fnv.suffix; // COMPLIANT (copy across structs), but safe.
43+
}
44+
45+
void internal_shift() {
46+
UnionSecret_t hash1;
47+
hash1.fnv.prefix = hash1.fnv.suffix; // COMPLIANT, same struct.
48+
}
49+
50+
void separate_access() {
51+
UnionSecret_t hash1, hash2;
52+
hash2.diff.suffix = hash1.fnv.suffix; // COMPLIANT, different union.
53+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <string.h>
2+
3+
int o[10];
4+
void g(void) {
5+
6+
o[2] = o[0]; // COMPLIANT
7+
8+
memcpy(&o[1], &o[0], 2); // NON_COMPLIANT
9+
memcpy(&o[2], &o[0], 2); // COMPLIANT
10+
memcpy(&o[2], &o[1], 2); // NON_COMPLIANT
11+
memcpy(o + 1, o, 2); // NON_COMPLIANT
12+
memcpy(o + 2, o, 2); // COMPLIANT
13+
memcpy(o + 2, o + 1, 2); // NON_COMPLIANT
14+
15+
// Exception 1
16+
int *p = &o[0];
17+
int *q = &o[0];
18+
19+
*p = *q; // COMPLIANT
20+
memcpy(&o[0], &o[0], 2); // COMPLIANT
21+
memcpy(o, o, 2); // COMPLIANT
22+
23+
// Exception 2
24+
memmove(&o[1], &o[0], 2u * sizeof(o[0])); // COMPLIANT
25+
}
26+
27+
struct s1 {
28+
int m1[10];
29+
};
30+
struct s2 {
31+
int m1;
32+
struct s1 m2;
33+
};
34+
union u {
35+
struct s1 m1;
36+
struct s2 m2;
37+
} u1;
38+
39+
typedef struct {
40+
char buf[8];
41+
} Union_t;
42+
union {
43+
unsigned char uc[24];
44+
struct {
45+
Union_t prefix;
46+
Union_t suffix;
47+
} fnv;
48+
struct {
49+
unsigned char padding[16];
50+
Union_t suffix;
51+
} diff;
52+
} u2;
53+
54+
void test_unions() {
55+
u1.m2.m2 = u1.m1; // NON_COMPLIANT
56+
57+
memcpy(&u1.m2.m2, &u1.m1, sizeof(u1.m1)); // NON_COMPLIANT
58+
memcpy(&u2.diff.suffix, &u2.fnv.suffix, sizeof(u2.fnv.suffix)); // COMPLIANT
59+
}

0 commit comments

Comments
 (0)