Skip to content

Commit cc0b8bb

Browse files
authored
Merge pull request github#12430 from MathiasVP/no-to-string-on-state-in-cast-array-pointer-arith
C++: Convert `cpp/upcast-array-pointer-arithmetic` to the new API
2 parents ce02de4 + f2b311a commit cc0b8bb

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

cpp/ql/src/Likely Bugs/Conversion/CastArrayPointerArithmetic.ql

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,39 @@
1818

1919
import cpp
2020
import semmle.code.cpp.ir.dataflow.DataFlow
21-
import DataFlow::PathGraph
21+
import CastToPointerArithFlow::PathGraph
2222

2323
Type getFullyConvertedType(DataFlow::Node node) {
2424
result = node.asExpr().getFullyConverted().getUnspecifiedType()
2525
}
2626

27-
class CastToPointerArithFlow extends DataFlow::Configuration {
28-
CastToPointerArithFlow() { this = "CastToPointerArithFlow" }
27+
module CastToPointerArithFlowConfig implements DataFlow::StateConfigSig {
28+
class FlowState = Type;
2929

30-
override predicate isSource(DataFlow::Node node, DataFlow::FlowState state) {
30+
predicate isSource(DataFlow::Node node, FlowState state) {
3131
not node.asExpr() instanceof Conversion and
3232
exists(Type baseType1, Type baseType2 |
3333
hasBaseType(node.asExpr(), baseType1) and
3434
hasBaseType(node.asExpr().getConversion*(), baseType2) and
3535
introducesNewField(baseType1, baseType2)
3636
) and
37-
getFullyConvertedType(node).getName() = state
37+
getFullyConvertedType(node) = state
3838
}
3939

40-
override predicate isSink(DataFlow::Node node, DataFlow::FlowState state) {
40+
predicate isSink(DataFlow::Node node, FlowState state) {
4141
(
4242
exists(PointerAddExpr pae | pae.getAnOperand() = node.asExpr()) or
4343
exists(ArrayExpr ae | ae.getArrayBase() = node.asExpr())
4444
) and
45-
getFullyConvertedType(node).getName() = state
45+
getFullyConvertedType(node) = state
46+
}
47+
48+
predicate isBarrier(DataFlow::Node node, FlowState state) { none() }
49+
50+
predicate isAdditionalFlowStep(
51+
DataFlow::Node node1, FlowState state1, DataFlow::Node node2, FlowState state2
52+
) {
53+
none()
4654
}
4755
}
4856

@@ -72,15 +80,9 @@ predicate introducesNewField(Class derived, Class base) {
7280
)
7381
}
7482

75-
pragma[nomagic]
76-
predicate hasFullyConvertedType(DataFlow::PathNode node, Type t) {
77-
getFullyConvertedType(node.getNode()) = t
78-
}
83+
module CastToPointerArithFlow = DataFlow::MakeWithState<CastToPointerArithFlowConfig>;
7984

80-
from DataFlow::PathNode source, DataFlow::PathNode sink, CastToPointerArithFlow cfg, Type t
81-
where
82-
cfg.hasFlowPath(pragma[only_bind_into](source), pragma[only_bind_into](sink)) and
83-
hasFullyConvertedType(source, t) and
84-
hasFullyConvertedType(sink, t)
85+
from CastToPointerArithFlow::PathNode source, CastToPointerArithFlow::PathNode sink
86+
where CastToPointerArithFlow::hasFlowPath(source, sink)
8587
select sink, source, sink, "This pointer arithmetic may be done with the wrong type because of $@.",
8688
source, "this cast"

0 commit comments

Comments
 (0)