`RULE-0-0-1`: "unreachable statement" false positives due to over-pruning of the control-flow graph

Open
#1,190 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
cpp
Domain
devtools

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

false positive/false negative
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from github/codeql-coding-standards

All issues in github/codeql-coding-standards

Similar issues

More DevTools issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.