Skip to content

Commit 3185856

Browse files
committed
Refine RULE-21-15
1 parent e0e0193 commit 3185856

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,35 @@ import cpp
1414
import codingstandards.c.misra
1515
import codingstandards.c.Pointers
1616

17-
class MemCmpMoveCpy extends BuiltInFunction {
18-
MemCmpMoveCpy() { this.getName().regexpMatch(".+mem(cmp|cpy|move).+") }
17+
class MemCmpMoveCpy extends Function {
18+
// Couldn't extend BuiltInFunction because it misses `memcmp`
19+
MemCmpMoveCpy() { this.getName().regexpMatch("mem(cmp|cpy|move)") }
20+
}
21+
22+
query predicate memfunArgTypes(FunctionCall fc, Type dstType, Type srcType) {
23+
(
24+
fc.getArgument(0).getUnspecifiedType() instanceof PointerType and
25+
fc.getArgument(1).getUnspecifiedType() instanceof PointerType
26+
or
27+
fc.getArgument(0).getUnspecifiedType() instanceof ArrayType and
28+
fc.getArgument(1).getUnspecifiedType() instanceof ArrayType
29+
) and
30+
dstType = fc.getArgument(0).getUnspecifiedType() and
31+
srcType = fc.getArgument(1).getUnspecifiedType()
1932
}
2033

2134
from FunctionCall fc
2235
where
2336
not isExcluded(fc,
2437
StandardLibraryFunctionTypesPackage::memcpyMemmoveMemcmpArgNotPointersToCompatibleTypesQuery()) and
2538
exists(MemCmpMoveCpy memfun | fc.getTarget() = memfun |
39+
(
40+
fc.getArgument(0).getUnspecifiedType() instanceof PointerType and
41+
fc.getArgument(1).getUnspecifiedType() instanceof PointerType
42+
or
43+
fc.getArgument(0).getUnspecifiedType() instanceof ArrayType and
44+
fc.getArgument(1).getUnspecifiedType() instanceof ArrayType
45+
) and
2646
fc.getArgument(0).getUnspecifiedType() = fc.getArgument(1).getUnspecifiedType()
2747
)
2848
select fc, fc.getArgument(0).getUnspecifiedType(), fc.getArgument(1).getUnspecifiedType()

c/misra/test/rules/RULE-21-15/test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ void sample() {
1010
memcpy(&from2, &to2, 2); // COMPLIANT
1111

1212
char from3[] = "string";
13-
char to3[6];
14-
memmove(from3, to3, 6); // COMPLIANT
13+
char to3[7];
14+
memmove(from3, to3, 7); // COMPLIANT
1515

1616
char from4[] = "sstringg";
1717
int to4[2];

0 commit comments

Comments
 (0)