Skip to content

Commit e0e0193

Browse files
committed
Draft of RULE-21-15
1 parent 5104114 commit e0e0193

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

c/misra/src/rules/RULE-21-15/MemcpyMemmoveMemcmpArgNotPointersToCompatibleTypes.ql

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@
1212

1313
import cpp
1414
import codingstandards.c.misra
15+
import codingstandards.c.Pointers
1516

16-
from Element x
17+
class MemCmpMoveCpy extends BuiltInFunction {
18+
MemCmpMoveCpy() { this.getName().regexpMatch(".+mem(cmp|cpy|move).+") }
19+
}
20+
21+
from FunctionCall fc
1722
where
18-
not isExcluded(x, StandardLibraryFunctionTypesPackage::memcpyMemmoveMemcmpArgNotPointersToCompatibleTypesQuery()) and
19-
any()
20-
select 1
23+
not isExcluded(fc,
24+
StandardLibraryFunctionTypesPackage::memcpyMemmoveMemcmpArgNotPointersToCompatibleTypesQuery()) and
25+
exists(MemCmpMoveCpy memfun | fc.getTarget() = memfun |
26+
fc.getArgument(0).getUnspecifiedType() = fc.getArgument(1).getUnspecifiedType()
27+
)
28+
select fc, fc.getArgument(0).getUnspecifiedType(), fc.getArgument(1).getUnspecifiedType()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <string.h>
2+
3+
void sample() {
4+
int from1 = 1000000;
5+
char to1;
6+
memcpy(&from1, &to1, 1); // NON_COMPLIANT, the types are not compatible
7+
8+
int from2 = 1000000;
9+
int to2;
10+
memcpy(&from2, &to2, 2); // COMPLIANT
11+
12+
char from3[] = "string";
13+
char to3[6];
14+
memmove(from3, to3, 6); // COMPLIANT
15+
16+
char from4[] = "sstringg";
17+
int to4[2];
18+
memmove(from4, to4, 8); // NON_COMPLIANT, despite being equal in byte counts
19+
20+
char from5[] = "STRING";
21+
char to5[] = "string";
22+
memcmp(from5, to5, 2); // COMPLIANT
23+
}
24+
25+
int main() { return 0; }

0 commit comments

Comments
 (0)