Skip to content

Commit ea40969

Browse files
committed
update ql files and add test.c for RULE-21-13
1 parent 13feea8 commit ea40969

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

c/misra/src/rules/RULE-21-13/CtypeFunctionArgNotUnsignedCharOrEof.ql

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,37 @@
1212

1313
import cpp
1414
import codingstandards.c.misra
15+
import codingstandards.cpp.ReadErrorsAndEOF
16+
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
17+
import semmle.code.cpp.rangeanalysis.RangeAnalysisUtils
18+
import semmle.code.cpp.dataflow.DataFlow // TODO use this...
1519

16-
from
20+
query predicate isCtypeFunction(Function function) {
21+
function.getADeclaration().getAFile().(HeaderFile).getShortName() = "_ctype" // TODO: change it back to `ctype`
22+
}
23+
24+
query predicate isInUnsignedCharRange(Expr var) {
25+
// TODO: shouldn't be an Expr, instead get it as an argument from a FunctionCall that isCtypeFunction
26+
exists(UnsignedCharType unsignedChar |
27+
// Consider cases where the argument's value is cast to some smaller type, clipping the range.
28+
typeLowerBound(unsignedChar) <= lowerBound(var.getFullyConverted()) and
29+
upperBound(var.getFullyConverted()) <= typeUpperBound(unsignedChar)
30+
)
31+
}
32+
33+
// Uh oh, this is empty
34+
query predicate isEOFInvocation(EOFInvocation eof) {
35+
any()
36+
}
37+
38+
/* very early draft */
39+
query predicate equivToEOF(FunctionCall fc, EOFInvocation eof) {
40+
// var is a param of ctypefunctioncall
41+
isCtypeFunction(fc.getTarget()) and
42+
DataFlow::localFlow(DataFlow::exprNode(eof.getExpr()), DataFlow::exprNode(fc.getArgument(0)))
43+
}
44+
from Element x
1745
where
1846
not isExcluded(x, StandardLibraryFunctionTypesPackage::ctypeFunctionArgNotUnsignedCharOrEofQuery()) and
19-
select
47+
any()
48+
select 1

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
import cpp
1414
import codingstandards.c.misra
1515

16-
from
16+
from Element x
1717
where
1818
not isExcluded(x, StandardLibraryFunctionTypesPackage::memcpyMemmoveMemcmpArgNotPointersToCompatibleTypesQuery()) and
19-
select
19+
any()
20+
select 1
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <stdio.h>
2+
#include <ctype.h>
3+
4+
void sample() {
5+
unsigned char c1 = 'c';
6+
int r1 = isalnum(c1); // compliant
7+
unsigned char c2 = EOF;
8+
int r2 = isalnum(c2); // compliant
9+
}
10+
11+
int main() { return 0; }

0 commit comments

Comments
 (0)