A Static Checker for Finding Smart Pointer Errors in C++ Programs
A static coding style checker for detecting API misuses of C++ smart pointers that will probably lead to memory errors like memory leak, use after free, and double free.
Report an auto_ptr
template argument used in an STL container.
std::vector<std::auto_ptr<int>> vi;
^
Warn here
Report all the memory transfer from one auto_ptr
to another.
std::auto_ptr<int> p1(new int(42));
void foo(std::auto_ptr<int> p);
foo(p1);
^
Warn here
Report the operation that delete
s the pointer from smart pointer observers.
std::unique_ptr<int> p = std::make_unique<int>(42);
delete p.get();
^
Warn here
Report the initiations with non-allocated memory.
int I;
std::unique_ptr<int> p(&I);
^
Warn here
Report a private auto_ptr
field in a class without copy constructors and assignment operators.
class Type {
private:
std::auto_ptr<int> p;
^
Warn here
};
Report the construction of a smart pointer with a raw pointer variable.
void foo(int *p) {
std::unique_ptr<int> sp(p);
^
Warn here
}
Report the mismatched type of template argument and new
operator for smart pointer constructions.
std::auto_ptr<int> sp(new int[42]);
^
Warn here
Report the usages of unchecked locked weak_ptr
s.
std::weak_ptr<int> wp;
...
*wp.lock() = 42;
^
Warn here
Report the release
d pointer that are not dealocated.
std::unique_ptr<int> sp;
...
*sp.release() = 42;
^
Warn here
clang-tidy with SPrinter (SHA256SUM 2c237c9a7e280f91d705ed6e16189c0a63b17643f09f3e233b2f7e6ecc712272)
Please contact us if you need the source code.
- Using clang-tidy.
- Using clang-tidy with IDEs or editors.
- To use SPrinter, please enable the
smartpointersafety-
prefix for all the error patterns mentioned above.
E.g.
$ clang-tidy -checks='-*,smartpointersafety-*' source.cpp