Merge pull request #13647 from purs3lab/uninitialized-local

C++: exclude uninitialized uses inside pure expression statements
This commit is contained in:
Jeroen Ketema
2023-07-11 10:31:33 +02:00
committed by GitHub
4 changed files with 51 additions and 0 deletions

View File

@@ -72,6 +72,11 @@ VariableAccess commonException() {
or
result.getParent() instanceof BuiltInOperation
or
// Ignore any uninitialized use that is explicitly cast to void and
// is an expression statement.
result.getActualType() instanceof VoidType and
result.getParent() instanceof ExprStmt
or
// Finally, exclude functions that contain assembly blocks. It's
// anyone's guess what happens in those.
containsInlineAssembly(result.getEnclosingFunction())

View File

@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* 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.

View File

@@ -14,3 +14,7 @@
| test.cpp:378:9:378:11 | val | The variable $@ may not be initialized at this access. | test.cpp:359:6:359:8 | val | val |
| test.cpp:417:10:417:10 | j | The variable $@ may not be initialized at this access. | test.cpp:414:9:414:9 | j | j |
| test.cpp:436:9:436:9 | j | The variable $@ may not be initialized at this access. | test.cpp:431:9:431:9 | j | j |
| test.cpp:454:2:454:2 | x | The variable $@ may not be initialized at this access. | test.cpp:452:6:452:6 | x | x |
| test.cpp:460:7:460:7 | x | The variable $@ may not be initialized at this access. | test.cpp:458:6:458:6 | x | x |
| test.cpp:467:2:467:2 | x | The variable $@ may not be initialized at this access. | test.cpp:464:6:464:6 | x | x |
| test.cpp:474:7:474:7 | x | The variable $@ may not be initialized at this access. | test.cpp:471:6:471:6 | x | x |

View File

@@ -435,3 +435,41 @@ int test38() {
return j; // BAD
}
void test39() {
int x;
x; // GOOD, in void context
}
void test40() {
int x;
(void)x; // GOOD, explicitly cast to void
}
void test41() {
int x;
x++; // BAD
}
void test42() {
int x;
void(x++); // BAD
}
void test43() {
int x;
int y = 1;
x + y; // BAD
}
void test44() {
int x;
int y = 1;
void(x + y); // BAD
}