Skip to content

Commit 358a8ab

Browse files
authored
Merge pull request github#8994 from HansmannThibaut/main
C/C++ : Wrong Uint access
2 parents 3745526 + ba28632 commit 358a8ab

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
void test()
2+
{
3+
uint16_t j = 256;
4+
char testSubject[122];
5+
6+
testSubject[j] = 12; // You can use a uint8 here
7+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
<overview>
7+
<p>Find access to an array with a Uint16 when the array has a size lower than 256.</p>
8+
</overview>
9+
10+
<recommendation>
11+
<p>Use a int with a lower bit size instead. For instance in this example use a 8 bit int.</p>
12+
</recommendation>
13+
14+
<example>
15+
<sample src="WrongUintAccess.cpp" />
16+
</example>
17+
18+
</qhelp>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @id cpp/wrong-uint-access
3+
* @name Wrong Uint
4+
* @descripion Acess an array of size lower than 256 with a uint16.
5+
* @kind problem
6+
* @problem.severity recommendation
7+
* @tags efficiency
8+
*/
9+
10+
import cpp
11+
12+
from Variable var, ArrayExpr useExpr, ArrayType defLine, VariableAccess use
13+
where
14+
var.getUnspecifiedType() = defLine and
15+
use = useExpr.getArrayBase() and
16+
var = use.getTarget() and
17+
(
18+
useExpr.getArrayOffset().getType() instanceof UInt16_t or
19+
useExpr.getArrayOffset().getType() instanceof UInt32_t or
20+
useExpr.getArrayOffset().getType() instanceof UInt64_t
21+
) and
22+
defLine.getArraySize() <= 256
23+
select useExpr,
24+
"Using a " + useExpr.getArrayOffset().getType() + " to acess the array $@ of size " +
25+
defLine.getArraySize() + ".", var, var.getName()

0 commit comments

Comments
 (0)