我在 CppCheck 中发现了错误吗?为什么会出现 "Null pointer dereference" 错误?

Did I find a bug in CppCheck? Why do I get the "Null pointer dereference" error here?

我正在使用 Cppcheck 来管理我的代码。我有以下功能:

bool my_function(std::string my_string) const
{
 return 0 == my_string.compare("Some text");  // line 3
}

令我惊讶的是,我在第 3 行空指针解引用

我完全糊涂了:我的函数中没有指针。为什么会出现此错误?

我试图调查它:

  1. 我检查了字符串my_string是否为空

  2. 我使用 "My text" 创建了一个对象,以确保 Cppcheck 不会抱怨使用临时对象:

    bool my_function(std::string my_string) const
    { 
     std::string str("Some text");
     return 0 == my_string.compare(str);  // line 3
    }
    

我还能做什么?是 Cppcheck 中的错误吗? compare函数本身有问题吗?如果是这种情况,我会感到惊讶,因为 cppcheck 不会抱怨我的项目中使用的任何其他 std 函数。

注意:我不是在询问 可能的空指针取消引用 错误,因此这不是以下任何问题的重复:1, 2 or

我是 Cppcheck 开发人员。

这看起来像是 Cppcheck 中的错误。但是我无法重现这种误报。如果您不使用最新的 Cppcheck,请更新。否则请在 cppcheck 错误跟踪器中报告:http://trac.cppcheck.net