Skip to content

Commit 298013a

Browse files
committed
C++: Add in-barrier on sources to reduce duplication.
1 parent 64d7b49 commit 298013a

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

cpp/ql/src/experimental/Security/CWE/CWE-193/ConstantSizeArrayOffByOne.ql

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,19 @@ predicate pointerArithOverflow0(
8484
pai.getElementSize() = f.getUnspecifiedType().(ArrayType).getBaseType().getSize() and
8585
f.getUnspecifiedType().(ArrayType).getArraySize() = size and
8686
semBounded(getSemanticExpr(pai.getRight()), any(SemZeroBound b), bound, true, _) and
87-
delta = bound - size
87+
delta = bound - size and
88+
delta >= 0 and
89+
size != 0 and
90+
size != 1
8891
}
8992

9093
module PointerArithmeticToDerefConfig implements DataFlow::ConfigSig {
9194
predicate isSource(DataFlow::Node source) {
9295
pointerArithOverflow0(source.asInstruction(), _, _, _, _)
9396
}
9497

98+
predicate isBarrierIn(DataFlow::Node node) { isSource(node) }
99+
95100
predicate isSink(DataFlow::Node sink) { isInvalidPointerDerefSink1(sink, _, _) }
96101
}
97102

@@ -127,18 +132,17 @@ module FieldAddressToDerefConfig implements DataFlow::StateConfigSig {
127132

128133
predicate isBarrier(DataFlow::Node node, FlowState state) { none() }
129134

135+
predicate isBarrierIn(DataFlow::Node node) { isSource(node, _) }
136+
130137
predicate isAdditionalFlowStep(
131138
DataFlow::Node node1, FlowState state1, DataFlow::Node node2, FlowState state2
132139
) {
133-
exists(PointerArithmeticInstruction pai, Field f, int size, int delta |
140+
exists(PointerArithmeticInstruction pai, Field f |
134141
state1 = TArray(f) and
135142
state2 = TOverflowArithmetic(pai) and
136143
pai.getLeft() = node1.asInstruction() and
137144
node2.asInstruction() = pai and
138-
pointerArithOverflow(pai, f, size, _, delta) and
139-
delta >= 0 and
140-
size != 0 and
141-
size != 1
145+
pointerArithOverflow(pai, f, _, _, _)
142146
)
143147
}
144148
}

0 commit comments

Comments
 (0)