Skip to content

Commit 83e26f4

Browse files
author
thibaut hansmann
committed
C/C++ : Wrong Uint access
1 parent 9d2f386 commit 83e26f4

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed
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 Uint8 instead</p>
12+
</recommendation>
13+
14+
<example>
15+
<sample src="WrongUintAcess.cpp" />
16+
</example>
17+
18+
</qhelp>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
import semmle.code.cpp.controlflow.SSA
12+
13+
from
14+
Variable E, ArrayExpr useExpr, ArrayType defExpr, VariableDeclarationEntry def, VariableAccess use
15+
where
16+
def = defExpr.getATypeNameUse() and
17+
E = def.getDeclaration() and
18+
use = useExpr.getArrayBase() and
19+
E = use.getTarget() and
20+
useExpr.getArrayOffset().getType() instanceof UInt16_t and
21+
defExpr.getArraySize() <= 256
22+
select useExpr, "Using a UInt16_t to acess the array $@ of size " + defExpr.getArraySize() + ".", E,
23+
E.getName()
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+
}

0 commit comments

Comments
 (0)