mirror of
https://github.com/github/codeql.git
synced 2026-04-28 18:25:24 +02:00
C/C++ : Wrong Uint access
This commit is contained in:
18
cpp/ql/src/experimental/Best Practices/WrongUintAccess.qhelp
Normal file
18
cpp/ql/src/experimental/Best Practices/WrongUintAccess.qhelp
Normal file
@@ -0,0 +1,18 @@
|
||||
<!DOCTYPE qhelp PUBLIC
|
||||
"-//Semmle//qhelp//EN"
|
||||
"qhelp.dtd">
|
||||
<qhelp>
|
||||
|
||||
<overview>
|
||||
<p>Find access to an array with a Uint16 when the array has a size lower than 256.</p>
|
||||
</overview>
|
||||
|
||||
<recommendation>
|
||||
<p>Use a Uint8 instead</p>
|
||||
</recommendation>
|
||||
|
||||
<example>
|
||||
<sample src="WrongUintAcess.cpp" />
|
||||
</example>
|
||||
|
||||
</qhelp>
|
||||
23
cpp/ql/src/experimental/Best Practices/WrongUintAccess.ql
Normal file
23
cpp/ql/src/experimental/Best Practices/WrongUintAccess.ql
Normal file
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* @id cpp/wrong-uint-access
|
||||
* @name Wrong Uint
|
||||
* @descripion Acess an array of size lower than 256 with a uint16.
|
||||
* @kind problem
|
||||
* @problem.severity recommendation
|
||||
* @tags efficiency
|
||||
*/
|
||||
|
||||
import cpp
|
||||
import semmle.code.cpp.controlflow.SSA
|
||||
|
||||
from
|
||||
Variable E, ArrayExpr useExpr, ArrayType defExpr, VariableDeclarationEntry def, VariableAccess use
|
||||
where
|
||||
def = defExpr.getATypeNameUse() and
|
||||
E = def.getDeclaration() and
|
||||
use = useExpr.getArrayBase() and
|
||||
E = use.getTarget() and
|
||||
useExpr.getArrayOffset().getType() instanceof UInt16_t and
|
||||
defExpr.getArraySize() <= 256
|
||||
select useExpr, "Using a UInt16_t to acess the array $@ of size " + defExpr.getArraySize() + ".", E,
|
||||
E.getName()
|
||||
@@ -0,0 +1,7 @@
|
||||
void test()
|
||||
{
|
||||
uint16_t j = 256;
|
||||
char testSubject[122];
|
||||
|
||||
testSubject[j] = 12; // You can use a uint8 here
|
||||
}
|
||||
Reference in New Issue
Block a user