Skip to content

Commit 97c2917

Browse files
authored
Merge pull request #6409 from JordyZomer/main
cpp: Add query to detect unsigned integer to signed integer conversio…
2 parents 2c41de6 + 0f6e845 commit 97c2917

File tree

1 file changed

+30
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)