@@ -3,12 +3,15 @@ private import semmle.code.cpp.commons.Scanf
3
3
private import semmle.code.cpp.controlflow.IRGuards
4
4
private import semmle.code.cpp.ir.ValueNumbering
5
5
6
+ private ConstantInstruction getZeroInstruction ( ) { result .getValue ( ) = "0" }
7
+
8
+ private Operand zero ( ) { result .getDef ( ) = getZeroInstruction ( ) }
9
+
6
10
private predicate exprInBooleanContext ( Expr e ) {
7
11
exists ( IRGuardCondition gc |
8
- exists ( Instruction i , ConstantInstruction zero |
9
- zero .getValue ( ) = "0" and
12
+ exists ( Instruction i |
10
13
i .getUnconvertedResultExpression ( ) = e and
11
- gc .comparesEq ( valueNumber ( i ) .getAUse ( ) , zero . getAUse ( ) , 0 , _, _)
14
+ gc .comparesEq ( valueNumber ( i ) .getAUse ( ) , zero ( ) , 0 , _, _)
12
15
)
13
16
or
14
17
gc .getUnconvertedResultExpression ( ) = e
@@ -33,15 +36,21 @@ private string getEofValue() {
33
36
)
34
37
}
35
38
39
+ private ConstantInstruction getEofInstruction ( ) { result .getValue ( ) = getEofValue ( ) }
40
+
41
+ private Operand eof ( ) { result .getDef ( ) = getEofInstruction ( ) }
42
+
36
43
/**
37
44
* Holds if the value of `call` has been checked to not equal `EOF`.
38
45
*/
39
46
private predicate checkedForEof ( ScanfFunctionCall call ) {
40
47
exists ( IRGuardCondition gc |
41
- exists ( Instruction i , ConstantInstruction eof |
42
- eof .getValue ( ) = getEofValue ( ) and
43
- i .getUnconvertedResultExpression ( ) = call and
44
- gc .comparesEq ( valueNumber ( i ) .getAUse ( ) , eof .getAUse ( ) , 0 , _, _)
48
+ exists ( Instruction i | i .getUnconvertedResultExpression ( ) = call |
49
+ // call == EOF
50
+ gc .comparesEq ( valueNumber ( i ) .getAUse ( ) , eof ( ) , 0 , _, _)
51
+ or
52
+ // call < 0 (EOF is guaranteed to be negative)
53
+ gc .comparesLt ( valueNumber ( i ) .getAUse ( ) , zero ( ) , 0 , true , _)
45
54
)
46
55
)
47
56
}
0 commit comments