[PATCH] D104300: [analyzer] Handle std::swap for std::unique_ptr

Deep Majumder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 15 11:23:53 PDT 2021


RedDocMD added a comment.

The current implementation of how notes are emitted in `handleSwap` is broken. Consider the following code:

  #include <memory>
  
  void foo() {
    auto ptr1 = std::unique_ptr<int>(new int(10));
    auto ptr2 = std::unique_ptr<int>(new int(13));
    ptr1.swap(ptr2);
    ptr1.reset();
    *ptr1;
  }

This yields the following  warning and notes:

  swap-and-reset.cpp:8:3: warning: Dereference of null smart pointer 'ptr1' [alpha.cplusplus.SmartPtr]
    *ptr1;
    ^~~~~
  swap-and-reset.cpp:4:36: note: Assigning 10
    auto ptr1 = std::unique_ptr<int>(new int(10));
                                     ^~~~~~~~~~~
  swap-and-reset.cpp:4:15: note: Smart pointer 'ptr1' is constructed
    auto ptr1 = std::unique_ptr<int>(new int(10));
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  swap-and-reset.cpp:5:36: note: Assigning 13
    auto ptr2 = std::unique_ptr<int>(new int(13));
                                     ^~~~~~~~~~~
  swap-and-reset.cpp:5:15: note: Smart pointer 'ptr2' is constructed
    auto ptr2 = std::unique_ptr<int>(new int(13));
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  swap-and-reset.cpp:6:3: note: Swapped null smart pointer 'ptr2' with smart pointer 'ptr1'
    ptr1.swap(ptr2);
    ^~~~~~~~~~~~~~~
  swap-and-reset.cpp:7:3: note: Smart pointer 'ptr1' reset using a null value
    ptr1.reset();
    ^~~~~~~~~~~~
  swap-and-reset.cpp:8:3: note: Dereference of null smart pointer 'ptr1'
    *ptr1;
    ^~~~~

But clearly, `ptr2` is not null.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D104300



More information about the cfe-commits mailing list