Skip to content

Commit 2587f8e

Browse files
committed
C++: Only alert on the largest possible overflow.
1 parent a09a8db commit 2587f8e

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

cpp/ql/src/experimental/Likely Bugs/OverrunWriteProductFlow.ql

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,34 @@ module StringSizeConfig implements ProductFlow::StateConfigSig {
134134

135135
module StringSizeFlow = ProductFlow::GlobalWithState<StringSizeConfig>;
136136

137+
int getOverflow(
138+
DataFlow::Node source1, DataFlow::Node source2, DataFlow::Node sink1, DataFlow::Node sink2,
139+
CallInstruction c, Expr buffer
140+
) {
141+
result > 0 and
142+
exists(
143+
StringSizeFlow::PathNode1 pathSource1, StringSizeFlow::PathNode2 pathSource2,
144+
StringSizeFlow::PathNode1 pathSink1, StringSizeFlow::PathNode2 pathSink2
145+
|
146+
StringSizeFlow::flowPath(pathSource1, pathSource2, pathSink1, pathSink2) and
147+
source1 = pathSource1.getNode() and
148+
source2 = pathSource2.getNode() and
149+
sink1 = pathSink1.getNode() and
150+
sink2 = pathSink2.getNode() and
151+
isSinkPairImpl(c, sink1, sink2, result + pathSink2.getState(), buffer)
152+
)
153+
}
154+
137155
from
138156
StringSizeFlow::PathNode1 source1, StringSizeFlow::PathNode2 source2,
139-
StringSizeFlow::PathNode1 sink1, StringSizeFlow::PathNode2 sink2, int overflow, int sinkState,
140-
CallInstruction c, DataFlow::Node sourceNode, Expr buffer, string element
157+
StringSizeFlow::PathNode1 sink1, StringSizeFlow::PathNode2 sink2, int overflow, CallInstruction c,
158+
Expr buffer, string element
141159
where
142160
StringSizeFlow::flowPath(source1, source2, sink1, sink2) and
143-
sinkState = sink2.getState() and
144-
isSinkPairImpl(c, sink1.getNode(), sink2.getNode(), overflow + sinkState, buffer) and
145-
overflow > 0 and
146-
sourceNode = source1.getNode() and
161+
overflow =
162+
max(getOverflow(source1.getNode(), source2.getNode(), sink1.getNode(), sink2.getNode(), c,
163+
buffer)
164+
) and
147165
if overflow = 1 then element = " element." else element = " elements."
148166
select c.getUnconvertedResultExpression(), source1, sink1,
149167
"This write may overflow $@ by " + overflow + element, buffer, buffer.toString()

0 commit comments

Comments
 (0)