SPrinter

A Static Checker for Finding Smart Pointer Errors in C++ Programs

Introduction

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.

Error Patterns

smartpointersafety-autoptr-in-container

Report an auto_ptr template argument used in an STL container.

std::vector<std::auto_ptr<int>> vi;
^
Warn here

smartpointersafety-autoptr-ownership-transfer

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

smartpointersafety-deallocating-observer-pointer

Report the operation that deletes the pointer from smart pointer observers.

std::unique_ptr<int> p = std::make_unique<int>(42);
delete p.get();
       ^
   Warn here

smartpointersafety-non-allocated-memory-initiation

Report the initiations with non-allocated memory.

int I;
std::unique_ptr<int> p(&I);
                       ^
                   Warn here

smartpointersafety-private-autoptr-in-class

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
};

smartpointersafety-raw-pointer-initiation

Report the construction of a smart pointer with a raw pointer variable.

void foo(int *p) {
    std::unique_ptr<int> sp(p);
                            ^
                        Warn here
}

smartpointersafety-type-mismatch-initiation

Report the mismatched type of template argument and new operator for smart pointer constructions.

std::auto_ptr<int> sp(new int[42]);
                      ^
                  Warn here

smartpointersafety-unchecked-locked-weak-pointer

Report the usages of unchecked locked weak_ptrs.

std::weak_ptr<int> wp;
   ...
*wp.lock() = 42;
^
Warn here

smartpointersafety-undeallocated-released-pointer

Report the released pointer that are not dealocated.

std::unique_ptr<int> sp;
   ...
*sp.release() = 42;
 ^
Warn here

Usage

Download

clang-tidy with SPrinter (SHA256SUM 2c237c9a7e280f91d705ed6e16189c0a63b17643f09f3e233b2f7e6ecc712272)

Please contact us if you need the source code.

Tool Usage

E.g.

$ clang-tidy -checks='-*,smartpointersafety-*' source.cpp

Publication