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:
I checked if the string my_string is empty.
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.
return (0 == my_string.compare("Some text"));
? Just maybe – Trevortrevorroperator==
and doreturn my_string == "Some text";
? – Supen