Skip to content

Commit 959724c

Browse files
committed
Add support for Array.Length in non SSA variable bounds
1 parent b51e63e commit 959724c

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

csharp/ql/src/semmle/code/csharp/dataflow/internal/rangeanalysis/BoundSpecific.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module Private {
1616

1717
private module Impl {
1818
private import csharp
19+
private import ConstantUtils
1920
private import semmle.code.csharp.dataflow.SSA::Ssa
2021

2122
/** Holds if this `v` is of type `IntegralType`. */
@@ -24,7 +25,7 @@ private module Impl {
2425
}
2526

2627
/** Holds if this `e` is a bound expression and it is not an SSA variable read. */
27-
predicate nonSsaVariableBoundedExpr(Expr e) { none() } // todo: we could add array.Length calls
28+
predicate nonSsaVariableBoundedExpr(Expr e) { systemArrayLengthAccess(e.(PropertyRead)) }
2829

2930
/** Gets an expression where SSA variable `v` is read. */
3031
Expr getARead(Definition v) { result = v.getARead() }

csharp/ql/src/semmle/code/csharp/dataflow/internal/rangeanalysis/ConstantUtils.qll

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ predicate propertyOverrides(Property p, string baseClass, string property) {
1717
)
1818
}
1919

20+
/**
21+
* Holds if `pa` is an access to the `Length` property of an array.
22+
*/
23+
predicate systemArrayLengthAccess(PropertyAccess pa) {
24+
propertyOverrides(pa.getTarget(), "System.Array", "Length")
25+
}
26+
2027
/**
2128
* Holds if expression `e` is either
2229
* - a compile time constant with integer value `val`, or
@@ -47,7 +54,7 @@ private int getArrayLengthRec(ArrayCreation arrCreation, int index) {
4754
}
4855

4956
private predicate isArrayLengthAccess(PropertyAccess pa, int length) {
50-
propertyOverrides(pa.getTarget(), "System.Array", "Length") and
57+
systemArrayLengthAccess(pa) and
5158
exists(ExplicitDefinition arr, ArrayCreation arrCreation |
5259
getArrayLengthRec(arrCreation, arrCreation.getNumberOfLengthArguments() - 1) = length and
5360
arrCreation = arr.getADefinition().getSource() and

0 commit comments

Comments
 (0)