Skip to content

Commit 044350b

Browse files
committed
Add CtypeGetcharFunctions modelling class
1 parent 9be18f2 commit 044350b

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

cpp/common/src/codingstandards/cpp/SimpleRangeAnalysisCustomizations.qll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,28 @@ private class CastEnumToIntegerSimpleRange extends SimpleRangeAnalysisExpr, Cast
151151
override predicate dependsOnChild(Expr child) { child = getExpr() }
152152
}
153153

154+
/**
155+
* <stdio.h> functions that reads a character from the STDIN,
156+
* or returns EOF if it fails to do so.
157+
* Their return type is `int` by their signatures, but
158+
* they actually return either an unsigned char or the EOF.
159+
*/
160+
private class CtypeGetcharFunctionsRange extends SimpleRangeAnalysisExpr, FunctionCall {
161+
CtypeGetcharFunctionsRange() {
162+
this.getFile().(HeaderFile).getBaseName() = "stdio.h" and
163+
this.getTarget().getName().regexpMatch("(fgetc|getc|getchar|)")
164+
}
165+
166+
/* It can return an EOF, which is -1 on most implementations. */
167+
override float getLowerBounds() { result = -1 }
168+
169+
/* Otherwise, it can return any unsigned char. */
170+
override float getUpperBounds() { result = 255 }
171+
172+
/* No, its call does not depend on any of its child. */
173+
override predicate dependsOnChild(Expr expr) { none() }
174+
}
175+
154176
/**
155177
* Gets the value of the expression `e`, if it is a constant.
156178
*

0 commit comments

Comments
 (0)