Skip to content

Commit b93a2b0

Browse files
committed
C++: prototype for off-by-one in array-typed field
1 parent 73f279d commit b93a2b0

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @id cpp/constant-size-array-off-by-one
3+
* @kind path-problem
4+
*/
5+
6+
import experimental.semmle.code.cpp.semantic.analysis.RangeAnalysis
7+
import experimental.semmle.code.cpp.rangeanalysis.Bound
8+
import experimental.semmle.code.cpp.semantic.SemanticExprSpecific
9+
import semmle.code.cpp.ir.IR
10+
import experimental.semmle.code.cpp.ir.dataflow.DataFlow
11+
12+
from
13+
FieldAddressInstruction fai, PointerArithmeticInstruction pai, AddressOperand ao, ZeroBound b,
14+
int delta, int size
15+
where
16+
size = fai.getField().getUnspecifiedType().(ArrayType).getArraySize() and
17+
DataFlow::localInstructionFlow(fai, pai.getLeft()) and
18+
DataFlow::localInstructionFlow(pai, ao.getAnyDef()) and
19+
semBounded(getSemanticExpr(pai.getRight()), b, delta, true, _) and
20+
delta >= size and
21+
size != 0 and // sometimes 0 or 1 is used for a variable-size array
22+
size != 1
23+
select pai, "This pointer may have an off-by-" + (delta - size) + "error allowing it to overrun $@",
24+
fai.getField(), fai.getField().toString()

0 commit comments

Comments
 (0)