FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
codeql/python/ql/src/Variables/MultiplyDefined.ql at codeql-cli/v2.26.3 · 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
458
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
/
python
/
ql
/
src
/
Variables
/
MultiplyDefined.ql
Copy path
More file actions
More file actions
Latest commit
History
History
History
72 lines (66 loc) · 1.95 KB
Breadcrumbs
codeql
/
python
/
ql
/
src
/
Variables
/
MultiplyDefined.ql
Copy path
File metadata and controls
72 lines (66 loc) · 1.95 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* @name Variable defined multiple times
* @description Assignment to a variable occurs multiple times without any intermediate use of that variable
* @kind problem
* @tags quality
* maintainability
* useless-code
* external/cwe/cwe-563
* @problem.severity warning
* @sub-severity low
* @precision very-high
* @id py/multiple-definition
*/
import
python
import
Definition
predicate
multiply_defined
(
AstNode
asgn1
,
AstNode
asgn2
,
Variable
v
)
{
/*
* Must be redefined on all possible paths in the CFG corresponding to the original source.
* For example, splitting may create a path where `def` is unconditionally redefined, even though
* it is not in the original source.
*/
forex
(
Definition
def
,
Definition
redef
|
def
.
getVariable
(
)
=
v
and
def
.
getNode
(
)
=
asgn1
and
redef
.
getNode
(
)
=
asgn2
|
def
.
isUnused
(
)
and
def
.
getARedef
(
)
=
redef
and
def
.
isRelevant
(
)
)
}
predicate
simple_literal
(
Expr
e
)
{
e
.
(
Num
)
.
getN
(
)
=
"0"
or
e
instanceof
NameConstant
or
e
instanceof
List
and
not
exists
(
e
.
(
List
)
.
getAnElt
(
)
)
or
e
instanceof
Tuple
and
not
exists
(
e
.
(
Tuple
)
.
getAnElt
(
)
)
or
e
instanceof
Dict
and
not
exists
(
e
.
(
Dict
)
.
getAKey
(
)
)
or
e
.
(
StringLiteral
)
.
getText
(
)
=
""
}
/**
* Holds if the redefinition is uninteresting.
*
* A multiple definition is 'uninteresting' if it sets a variable to a
* simple literal before reassigning it.
* x = None
* if cond:
* x = value1
* else:
* x = value2
*/
predicate
uninteresting_definition
(
AstNode
asgn1
)
{
exists
(
AssignStmt
a
|
a
.
getATarget
(
)
=
asgn1
|
simple_literal
(
a
.
getValue
(
)
)
)
}
from
AstNode
asgn1
,
AstNode
asgn2
,
Variable
v
where
multiply_defined
(
asgn1
,
asgn2
,
v
)
and
forall
(
Name
el
|
el
=
asgn1
.
getParentNode
(
)
.
(
Tuple
)
.
getAnElt
(
)
|
multiply_defined
(
el
,
_
,
_
)
)
and
not
uninteresting_definition
(
asgn1
)
select
asgn1
,
"This assignment to '"
+
v
.
getId
(
)
+
"' is unnecessary as it is $@ before this value is used."
,
asgn2
,
"redefined"
Back
|
FazBrowse Home
|
New Git URL