Skip to content

Commit e075165

Browse files
committed
cpp: Add query to detect unsigned integer to signed integer conversions used in pointer arithmetics
1 parent 8ce6335 commit e075165

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @author Jordy Zomer
3+
* @name unsiged to signed used in pointer arithmetic
4+
* @description finds unsigned to signed conversions used in pointer arithmetic, potentially causing an out-of-bound access
5+
* @id cpp/out-of-bounds
6+
* @kind problem
7+
* @problem.severity warning
8+
* @tags reliability
9+
* security
10+
* external/cwe/cwe-787
11+
*/
12+
13+
import cpp
14+
import semmle.code.cpp.dataflow.DataFlow
15+
import semmle.code.cpp.security.Overflow
16+
17+
from FunctionCall call, Function f, Parameter p, DataFlow::Node sink, PointerArithmeticOperation pao, Operation a, Operation b
18+
where
19+
f = call.getTarget() and
20+
p = f.getAParameter() and
21+
p.getType().getUnderlyingType().(IntegralType).isSigned() and
22+
call.getArgument(p.getIndex()).getType().getUnderlyingType().(IntegralType).isUnsigned() and
23+
pao.getAnOperand() = sink.asExpr() and
24+
not guardedLesser(a, sink.asExpr()) and
25+
not guardedGreater(b, call.getArgument(p.getIndex())) and
26+
not call.getArgument(p.getIndex()).isConstant() and
27+
DataFlow::localFlow(DataFlow::parameterNode(p), sink)
28+
select call, "This call: $@ passes an unsigned int to a function that requires a signed int: $@. And then used in pointer arithmetic: $@", call, call.toString(), f, f.toString(), sink, sink.toString()

0 commit comments

Comments
 (0)