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

未关闭
#1,190 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
cpp
领域
devtools

调研方向

从 RULE-0-0-1 的自定义 isReachable 谓词开始,跟踪 BasicBlock.getAPredecessor()、possiblePredecessor、potentiallyReturningFunction 和 reachableRecursive。使用 CodeQL CLI 2.26.2 通过自包含的 C++ 示例重现该报告,然后验证 update() 和 run() 中可达的语句不再被报告为不可达。

由索引模型根据 Issue 内容生成。

描述

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;
}

主要语言
CodeQL
星标
227
派生
82
平均合并
6 天 7 小时
30 天内合并 PR
9

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

github/codeql-coding-standards 的其他 Issue

查看 github/codeql-coding-standards 的全部 Issue

相似的 Issue

更多 DevTools Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。