简体   繁体   中英

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

I'm using Cppcheck to manage my code. I have the following function:

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

To my surprise, I get the Null pointer dereference in the line 3.

I am completely confused: there are no pointers in my function. Why do I get this error?

I've tried to investigate it:

  1. I checked if the string my_string is empty.

  2. I created an object using "My text" to make sure that Cppcheck doesn't complain about using a temporary object:

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

What else can I do? Is is a bug in Cppcheck? Is there a problem with the compare function itself? I'd be surprised if this was the case, since cppcheck doesn't complain about any other std functions that are used in my project.

Note: I'm not asking about the possible Null pointer dereference error, so this is not a duplicate of any of the following questions: 1 , 2 or 3 .

I am a Cppcheck developer.

It looks like a bug in Cppcheck. However I fail to reproduce this false positive. If you don't use latest Cppcheck please update. Otherwise please report this in the cppcheck bug tracker: http://trac.cppcheck.net

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM