gpt4 book ai didi

c++ - 在 PVS-Studio 中的 BOOST_REQUIRE 之后将变量标记为非 NULL

转载 作者:搜寻专家 更新时间:2023-10-31 02:08:40 26 4
gpt4 key购买 nike

我正在使用 PVS-Studio 来分析我的测试代码。通常有以下形式的结构

const noAnimal* animal = dynamic_cast<noAnimal*>(...);
BOOST_REQUIRE(animal);
BOOST_REQUIRE_EQUAL(animal->GetSpecies(), ...);

但是我仍然收到警告 V522 There might be dereferencing of a potential null pointer 'animal' 最后一行。

我知道可以将函数标记为“不返回 NULL”,但是否也可以将函数标记为有效的 NULL 检查或以其他方式让 PVS-Studio 意识到 animal 可以BOOST_REQUIRE(animal); 之后不是 NULL 吗?

如果首先通过任何 assert flavor 检查指针,也会发生这种情况。

最佳答案

感谢您提供有趣的示例。我们会想,我们可以用 BOOST_REQUIRE 宏做什么。

目前,我可以建议您解决以下问题:

之后的某个地方

#include <boost/test/included/unit_test.hpp>

你可以这样写:

#ifdef PVS_STUDIO
#undef BOOST_REQUIRE
#define BOOST_REQUIRE(expr) do { if (!(expr)) throw "PVS-Studio"; } while (0)
#endif

这样,您将向分析器提示错误条件会导致控制流中止。这不是最漂亮的解决方案,但我认为值得告诉您。

关于c++ - 在 PVS-Studio 中的 BOOST_REQUIRE 之后将变量标记为非 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47283349/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com