[PATCH] D55388: [analyzer] MoveChecker Pt.8: Add checks for dereferencing a smart pointer after move.

Henry Wong via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 9 06:45:42 PST 2018


MTC added a comment.

In D55388#1322601 <https://reviews.llvm.org/D55388#1322601>, @xazax.hun wrote:

> Hm. I wonder if it would also make sense to model e.g. the get method to return nullptr for moved from smart ptrs. This could help null dereference checker and also aid false path prunning.


Great idea!

IIRC, we haven't model smart-pointer yet. There are no warnings fired in the simple code below.

  int foo() {
      auto ptr = std::make_unique<int>(0);
      ptr = nullptr;
      return *ptr;              // <--------- Should emit a warning here
  }

If we want to model the `get()` method, not only the move action but also the assignment should be considered. `std::unique_ptr` may be fine, but is the reference count stuff too complex for `std::shared_ptr` to get the information available for analysis? Can the `lifetime` analysis cover the smart pointers?

  int unique_ptr_bad() {
      auto ptr = std::make_unique<int>(0);
      ptr = nullptr;
      int *raw_p = ptr.get();   // <--------- 'raw_p' is definitely null here
      return *raw_p;              // <--------- Should emit a warning here
  }

Related dev-mail about smart pointer checkers, see http://lists.llvm.org/pipermail/cfe-dev/2012-January/019446.html


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D55388/new/

https://reviews.llvm.org/D55388





More information about the cfe-commits mailing list