Skip to content

Commit d217e1e

Browse files
authored
Merge pull request github#13647 from purs3lab/uninitialized-local
C++: exclude uninitialized uses inside pure expression statements
2 parents 93ad204 + d8e0ffa commit d217e1e

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

cpp/ql/src/Likely Bugs/Memory Management/UninitializedLocal.ql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ VariableAccess commonException() {
7272
or
7373
result.getParent() instanceof BuiltInOperation
7474
or
75+
// Ignore any uninitialized use that is explicitly cast to void and
76+
// is an expression statement.
77+
result.getActualType() instanceof VoidType and
78+
result.getParent() instanceof ExprStmt
79+
or
7580
// Finally, exclude functions that contain assembly blocks. It's
7681
// anyone's guess what happens in those.
7782
containsInlineAssembly(result.getEnclosingFunction())
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The `cpp/uninitialized-local` query now excludes uninitialized uses that are explicitly cast to void and are expression statements. As a result, the query will report less false positives.

cpp/ql/test/query-tests/Security/CWE/CWE-457/semmle/tests/UninitializedLocal.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@
1414
| test.cpp:378:9:378:11 | val | The variable $@ may not be initialized at this access. | test.cpp:359:6:359:8 | val | val |
1515
| test.cpp:417:10:417:10 | j | The variable $@ may not be initialized at this access. | test.cpp:414:9:414:9 | j | j |
1616
| test.cpp:436:9:436:9 | j | The variable $@ may not be initialized at this access. | test.cpp:431:9:431:9 | j | j |
17+
| test.cpp:454:2:454:2 | x | The variable $@ may not be initialized at this access. | test.cpp:452:6:452:6 | x | x |
18+
| test.cpp:460:7:460:7 | x | The variable $@ may not be initialized at this access. | test.cpp:458:6:458:6 | x | x |
19+
| test.cpp:467:2:467:2 | x | The variable $@ may not be initialized at this access. | test.cpp:464:6:464:6 | x | x |
20+
| test.cpp:474:7:474:7 | x | The variable $@ may not be initialized at this access. | test.cpp:471:6:471:6 | x | x |

cpp/ql/test/query-tests/Security/CWE/CWE-457/semmle/tests/test.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,3 +435,41 @@ int test38() {
435435

436436
return j; // BAD
437437
}
438+
439+
void test39() {
440+
int x;
441+
442+
x; // GOOD, in void context
443+
}
444+
445+
void test40() {
446+
int x;
447+
448+
(void)x; // GOOD, explicitly cast to void
449+
}
450+
451+
void test41() {
452+
int x;
453+
454+
x++; // BAD
455+
}
456+
457+
void test42() {
458+
int x;
459+
460+
void(x++); // BAD
461+
}
462+
463+
void test43() {
464+
int x;
465+
int y = 1;
466+
467+
x + y; // BAD
468+
}
469+
470+
void test44() {
471+
int x;
472+
int y = 1;
473+
474+
void(x + y); // BAD
475+
}

0 commit comments

Comments
 (0)