`RULE-0-0-1`: "unreachable statement" false positives due to over-pruning of the control-flow graph
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 48/100
Research direction
Start at RULE-0-0-1's custom isReachable predicate and trace BasicBlock.getAPredecessor(), possiblePredecessor, potentiallyReturningFunction, and reachableRecursive. Reproduce the report with the self-contained C++ example using CodeQL CLI 2.26.2, then verify that reachable statements in update() and run() are no longer reported as unreachable.
Written by the indexing model from the issue text.
Description
Affected rules
RULE-0-0-1
Description
RULE-0-0-1 reports plainly reachable statements as unreachable. The query does not use the standard reachable predicate; it defines its own reachability walk over BasicBlock.getAPredecessor():
predicate isReachable(BasicBlock bb) {
bb = any(Function f).getEntryPoint()
or
isReachable(bb.getAPredecessor())
or
... // special cases for &&/||, ?:, catch blocks, constexpr if
}
BasicBlock.getAPredecessor() is derived from successors_adapted , One pruning step, possiblePredecessor, removes the control-flow graph edge after a FunctionCall unless the callee is classified as potentiallyReturningFunction. That classification is itself defined recursively in terms of the adapted control-flow graph (reachableRecursive).
When this circular analysis fails to prove that a function returns the edge after the call is dropped, the rest of the function becomes "unreachable", and the function's own exit point is then considered unreachable too. The function is consequently treated as noreturn, so callers have everything after the call flagged as well. The error cascades across the translation unit.
Environment: CodeQL CLI 2.26.2, codeql/misra-cpp-coding-standards 2.62.0 (cpp-all 5.0.0), C++17.
Example
Five false positives on this self-contained file: the if in update() and every statement in run() after the call to update().
#include <unordered_map>
namespace detail {
struct Error {
Error() noexcept = default;
};
template <typename E>
struct unexpected {
E error;
explicit unexpected(E e) noexcept : error(e) {}
};
template <typename T, typename E>
struct expected {
T value_{};
E error_{};
bool has_value_ = true;
expected(T v) noexcept : value_(v), has_value_(true) {}
expected(unexpected<E> u) noexcept : error_(u.error), has_value_(false) {}
explicit operator bool() const noexcept { return has_value_; }
};
} // namespace detail
using Error = detail::Error;
using Unexpected = detail::unexpected<Error>;
template <typename T> using Result = detail::expected<T, Error>;
Result<int> update(const std::unordered_map<int, int>& keys) noexcept {
auto it = keys.find(0);
if (it == keys.end()) { // flagged as unreachable
return Result<int>{Unexpected{Error{}}};
}
return Result<int>{it->second};
}
void run() noexcept {
std::unordered_map<int, int> keys;
auto r = update(keys); // flagged as unreachable
if (r) { // flagged as unreachable
(void)0; // flagged as unreachable
}
int x = 42; // flagged as unreachable
(void)x;
}
- Dominant language
- CodeQL
- Stars
- 227
- Forks
- 82
- Avg merge
- 6d 7h
- Merged PRs (30d)
- 9
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from github/codeql-coding-standards
-
false positive/false negative Stardard-MISRA-C++
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
github/codeql-coding-standards#1172 ·
-
Difficulty-Low false positive/false negative false-negative Impact-Low Standard-MISRA-C
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Difficulty-Medium false positive/false negative false-positive Impact-Medium Standard-CERT-C
Difficulty 4/5 3-5 days Newbie friendliness 48/100
github/codeql-coding-standards#1200 ·
-
false positive/false negative
Difficulty 3/5 1-2 days Newbie friendliness 65/100
github/codeql-coding-standards#1175 ·
-
false positive/false negative Stardard-MISRA-C++
Difficulty 3/5 1-2 days Newbie friendliness 48/100
github/codeql-coding-standards#1165 ·
All issues in github/codeql-coding-standards
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
hemilabs/ui-monorepo#2332 ·
-
Help-Wanted Needs-Triage Package-Update
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
microsoft/winget-pkgs#438662 ·
-
priority: p3
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
googleapis/librarian#7636 ·
-
bug good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
olcf/olcf-test-harness#278 · 1 comment ·