Skip to content

Commit 5104114

Browse files
committed
Refine RULE-21-13 and its unit test
1 parent cf0d756 commit 5104114

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

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

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,23 @@ class CtypeFunction extends Function {
2020
CtypeFunction() { this.getADeclaration().getAFile().(HeaderFile).getShortName() = "_ctype" }
2121
}
2222

23-
predicate unsignedCharRange(int lower, int upper, EOFInvocation eof) {
24-
exists(UnsignedCharType unsignedChar |
25-
lower = typeLowerBound(unsignedChar) and
26-
upper = upperBound(eof.getExpr()) and
27-
typeLowerBound(unsignedChar) <= lowerBound(eof.getExpr()) and
28-
upperBound(eof.getExpr()) <= typeUpperBound(unsignedChar)
29-
)
30-
}
31-
32-
predicate isEquivToEOF(Expr expr) {
33-
exists(EOFInvocation eof | DataFlow::localFlow(DataFlow::exprNode(eof.getExpr()), DataFlow::exprNode(expr)))
34-
}
35-
3623
from FunctionCall ctypeCall
3724
where
3825
not isExcluded(ctypeCall,
3926
StandardLibraryFunctionTypesPackage::ctypeFunctionArgNotUnsignedCharOrEofQuery()) and
40-
exists(CtypeFunction ctype, UnsignedCharType unsignedChar |
27+
not exists(CtypeFunction ctype, UnsignedCharType unsignedChar |
4128
ctypeCall = ctype.getACallToThisFunction()
4229
|
43-
/* The argument's value should be in the `unsigned char` range. */
30+
/* The argument's value should be in the `unsigned char` range. */
4431
typeLowerBound(unsignedChar) <= lowerBound(ctypeCall.getAnArgument().getExplicitlyConverted()) and // consider casts
4532
upperBound(ctypeCall.getAnArgument().getExplicitlyConverted()) <= typeUpperBound(unsignedChar)
4633
or
47-
/* The argument's value is reachable from EOF. */
48-
exists(EOFInvocation eof | DataFlow::localFlow(DataFlow::exprNode(eof.getExpr()), DataFlow::exprNode(ctypeCall.getAnArgument())))
34+
/* The argument's value is reachable from EOF. */
35+
exists(EOFInvocation eof |
36+
DataFlow::localFlow(DataFlow::exprNode(eof.getExpr()),
37+
DataFlow::exprNode(ctypeCall.getAnArgument()))
38+
)
4939
)
50-
select ctypeCall, ctypeCall.getAnArgument()
40+
select ctypeCall,
41+
"The <ctype.h> function $@ accepts an argument $@ that is not unsigned char nor an EOF.",
42+
ctypeCall, ctypeCall.getTarget(), ctypeCall.getAnArgument(), ctypeCall.getAnArgument().toString()

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@ void sample() {
66
int r1 = isalnum(c1); // COMPLIANT: ASCII 99 is within unsigned char range of [0, 255]
77
unsigned char x1 = EOF;
88
unsigned char x2 = x1;
9-
unsigned char c2 = x2 + 1;
9+
unsigned char c2 = x2;
1010
int r2 = isdigit(c2); // COMPLIANT: EOF (-1)
1111

1212
int x3 = 256;
1313
int x4 = x3;
1414
int c3 = x4;
1515
int r3 = islower(c3); // NON_COMPLIANT: is outside unsigned char range of[0, 255]
16+
17+
unsigned char x5 = EOF;
18+
unsigned char x6 = x5;
19+
int c4 = x6 + 10000;
20+
int r4 = isdigit(c4); // NON_COMPLIANT: is outside unsigned char range of[0, 255]
1621
}
1722

1823
int main() { return 0; }

0 commit comments

Comments
 (0)