cppcheck seems to not recognize overloaded operator=(const char*) and similar functions, which do not save their pointer argument.
#include <iostream> #include <cstring> #include <string> struct String { char* p{}; String() = default; String(const String&) = delete; String(String&&) = delete; ~String() { delete p; } String& operator=(const String&) = delete; String& operator=(const char* s) { if (s == nullptr || s == p) return *this; const size_t length = strlen(s) + 1; delete p; p = new char[length]; strncpy(p, s, length); return *this; } }; std::string bar() { return "bar"; } bool foo() { String S; S = bar().c_str(); std::cout << S.p << '\n'; // Using object to temporary. [danglingTemporaryLifetime] return S.p != nullptr; } int main() { return foo() ? EXIT_SUCCESS : EXIT_FAILURE; }
Running on the latest from git, I dont see an error.
That's nice to hear, looking forward to v2.2.
Log in to post a comment.
cppcheck seems to not recognize overloaded operator=(const char*) and similar functions, which do not save their pointer argument.
Running on the latest from git, I dont see an error.
That's nice to hear, looking forward to v2.2.