@@ -20,6 +20,7 @@ import semmle.code.cpp.models.interfaces.ArrayFunction
2020import semmle.code.cpp.rangeanalysis.new.internal.semantic.analysis.RangeAnalysis
2121import semmle.code.cpp.rangeanalysis.new.internal.semantic.SemanticExprSpecific
2222import StringSizeFlow:: PathGraph1
23+ import codeql.util.Unit
2324
2425pragma [ nomagic]
2526Instruction getABoundIn ( SemBound b , IRFunction func ) {
@@ -46,7 +47,7 @@ VariableAccess getAVariableAccess(Expr e) { e.getAChild*() = result }
4647 * Holds if `(n, state)` pair represents the source of flow for the size
4748 * expression associated with `alloc`.
4849 */
49- predicate hasSize ( AllocationExpr alloc , DataFlow:: Node n , string state ) {
50+ predicate hasSize ( AllocationExpr alloc , DataFlow:: Node n , int state ) {
5051 exists ( VariableAccess va , Expr size , int delta |
5152 size = alloc .getSizeExpr ( ) and
5253 // Get the unique variable in a size expression like `x` in `malloc(x + 1)`.
@@ -55,7 +56,7 @@ predicate hasSize(AllocationExpr alloc, DataFlow::Node n, string state) {
5556 bounded ( any ( Instruction instr | instr .getUnconvertedResultExpression ( ) = size ) ,
5657 any ( LoadInstruction load | load .getUnconvertedResultExpression ( ) = va ) , delta ) and
5758 n .asConvertedExpr ( ) = va .getFullyConverted ( ) and
58- state = delta . toString ( )
59+ state = delta
5960 )
6061}
6162
@@ -78,9 +79,9 @@ predicate isSinkPairImpl(
7879}
7980
8081module StringSizeConfig implements ProductFlow:: StateConfigSig {
81- class FlowState1 = string ;
82+ class FlowState1 = Unit ;
8283
83- class FlowState2 = string ;
84+ class FlowState2 = int ;
8485
8586 predicate isSourcePair (
8687 DataFlow:: Node bufSource , FlowState1 state1 , DataFlow:: Node sizeSource , FlowState2 state2
@@ -91,18 +92,18 @@ module StringSizeConfig implements ProductFlow::StateConfigSig {
9192 // ```
9293 // we use `state2` to remember that there was an offset (in this case an offset of `1`) added
9394 // to the size of the allocation. This state is then checked in `isSinkPair`.
94- state1 instanceof DataFlow :: FlowStateEmpty and
95+ exists ( state1 ) and
9596 hasSize ( bufSource .asConvertedExpr ( ) , sizeSource , state2 )
9697 }
9798
9899 predicate isSinkPair (
99100 DataFlow:: Node bufSink , FlowState1 state1 , DataFlow:: Node sizeSink , FlowState2 state2
100101 ) {
101- state1 instanceof DataFlow :: FlowStateEmpty and
102- state2 = [ - 32 .. 32 ] . toString ( ) and // An arbitrary bound because we need to bound `state2`
102+ exists ( state1 ) and
103+ state2 = [ - 32 .. 32 ] and // An arbitrary bound because we need to bound `state2`
103104 exists ( int delta |
104105 isSinkPairImpl ( _, bufSink , sizeSink , delta , _) and
105- delta > state2 . toInt ( )
106+ delta > state2
106107 )
107108 }
108109
@@ -111,18 +112,18 @@ module StringSizeConfig implements ProductFlow::StateConfigSig {
111112 predicate isBarrier2 ( DataFlow:: Node node , FlowState2 state ) { none ( ) }
112113
113114 predicate isAdditionalFlowStep1 (
114- DataFlow:: Node node1 , FlowState1 state1 , DataFlow:: Node node2 , FlowState2 state2
115+ DataFlow:: Node node1 , FlowState1 state1 , DataFlow:: Node node2 , FlowState1 state2
115116 ) {
116117 none ( )
117118 }
118119
119120 predicate isAdditionalFlowStep2 (
120- DataFlow:: Node node1 , FlowState1 state1 , DataFlow:: Node node2 , FlowState2 state2
121+ DataFlow:: Node node1 , FlowState2 state1 , DataFlow:: Node node2 , FlowState2 state2
121122 ) {
122123 exists ( AddInstruction add , Operand op , int delta , int s1 , int s2 |
123124 s1 = [ - 32 .. 32 ] and // An arbitrary bound because we need to bound `state`
124- state1 = s1 . toString ( ) and
125- state2 = s2 . toString ( ) and
125+ state1 = s1 and
126+ state2 = s2 and
126127 add .hasOperands ( node1 .asOperand ( ) , op ) and
127128 semBounded ( getSemanticExpr ( op .getDef ( ) ) , any ( SemZeroBound zero ) , delta , true , _) and
128129 node2 .asInstruction ( ) = add and
139140 CallInstruction c , DataFlow:: Node sourceNode , Expr buffer , string element
140141where
141142 StringSizeFlow:: hasFlowPath ( source1 , source2 , sink1 , sink2 ) and
142- sinkState = sink2 .getState ( ) . toInt ( ) and
143+ sinkState = sink2 .getState ( ) and
143144 isSinkPairImpl ( c , sink1 .getNode ( ) , sink2 .getNode ( ) , overflow + sinkState , buffer ) and
144145 overflow > 0 and
145146 sourceNode = source1 .getNode ( ) and
0 commit comments