FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
codeql/cpp/ql/src/Critical/SizeCheck.ql at codeql-cli/v2.19.2 · github/codeql · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
github
/
codeql
Public
Notifications
You must be signed in to change notification settings
Fork
2.1k
Star
10k
Code
Issues
997
Pull requests
467
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
codeql
/
cpp
/
ql
/
src
/
Critical
/
SizeCheck.ql
Copy path
More file actions
More file actions
Latest commit
History
History
History
46 lines (42 loc) · 1.46 KB
Breadcrumbs
codeql
/
cpp
/
ql
/
src
/
Critical
/
SizeCheck.ql
Copy path
File metadata and controls
46 lines (42 loc) · 1.46 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* @name Not enough memory allocated for pointer type
* @description Calling 'malloc', 'calloc' or 'realloc' without allocating enough memory to contain
* an instance of the type of the pointer may result in a buffer overflow
* @kind problem
* @problem.severity warning
* @security-severity 8.1
* @precision medium
* @id cpp/allocation-too-small
* @tags reliability
* security
* external/cwe/cwe-131
* external/cwe/cwe-122
*/
import
cpp
import
semmle.code.cpp.models.Models
predicate
baseType
(
AllocationExpr
alloc
,
Type
base
)
{
exists
(
PointerType
pointer
|
pointer
.
getBaseType
(
)
=
base
and
(
exists
(
AssignExpr
assign
|
assign
.
getRValue
(
)
=
alloc
and
assign
.
getLValue
(
)
.
getType
(
)
=
pointer
)
or
exists
(
Variable
v
|
v
.
getInitializer
(
)
.
getExpr
(
)
=
alloc
and
v
.
getType
(
)
=
pointer
)
)
)
}
predicate
decideOnSize
(
Type
t
,
int
size
)
{
// If the codebase has more than one type with the same name, it can have more than one size.
size
=
min
(
t
.
getSize
(
)
)
}
from
AllocationExpr
alloc
,
Type
base
,
int
basesize
,
int
allocated
where
baseType
(
alloc
,
base
)
and
allocated
=
alloc
.
getSizeBytes
(
)
and
decideOnSize
(
base
,
basesize
)
and
alloc
.
(
FunctionCall
)
.
getTarget
(
)
instanceof
AllocationFunction
and
// exclude `new` and similar
basesize
>
allocated
select
alloc
,
"Type '"
+
base
.
getName
(
)
+
"' is "
+
basesize
.
toString
(
)
+
" bytes, but only "
+
allocated
.
toString
(
)
+
" bytes are allocated."
Back
|
FazBrowse Home
|
New Git URL