Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++: Improvements to reduce false alarms #16149

Merged
merged 10 commits into from
Apr 16, 2024
17 changes: 16 additions & 1 deletion cpp/ql/src/Critical/GlobalUseBeforeInit.ql
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,25 @@ predicate callReaches(Call call, ControlFlowNode successor) {
)
}

// To avoid many false alarms like `static int a = 1;`
predicate initialisedAtDeclaration(GlobalVariable v) {
exists(VariableDeclarationEntry vde |
vde = v.getDefinition()
and vde.isDefinition()
)
}
MathiasVP marked this conversation as resolved.
Show resolved Hide resolved

// No need to initialize those variables
predicate isStdlibVariable(GlobalVariable v) {
v.getName() = ["stdin", "stdout", "stderr"]
}
MathiasVP marked this conversation as resolved.
Show resolved Hide resolved

from GlobalVariable v, Function f
where
uninitialisedBefore(v, f) and
useFunc(v, f)
useFunc(v, f) and
not initialisedAtDeclaration(v) and
not isStdlibVariable(v)
MathiasVP marked this conversation as resolved.
Show resolved Hide resolved
select f,
"The variable '" + v.getName() +
" is used in this function but may not be initialized when it is called."
2 changes: 2 additions & 0 deletions cpp/ql/src/Critical/InconsistentNullnessTesting.ql
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import cpp
from StackVariable v, ControlFlowNode def, VariableAccess checked, VariableAccess unchecked
where
checked = v.getAnAccess() and
// The check can often be in a macro for handling exception
not checked.isInMacroExpansion() and
dereferenced(checked) and
unchecked = v.getAnAccess() and
dereferenced(unchecked) and
Expand Down
6 changes: 6 additions & 0 deletions cpp/ql/src/change-notes/2024-04-09-reduce-FP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: majorAnalysis
---
* Reduce false positives of `GlobalUseBeforeInit.ql` and `InconsistentNullnessTesting.ql`.
Many global variables that are initialized at declaration will be reported by `GlobalUseBeforeInit.ql`.
When `checked` is in a macro expansion for handling exceptions, it is very likely for `InconsistentNullnessTesting.ql` to report false positives.
MathiasVP marked this conversation as resolved.
Show resolved Hide resolved
Loading