<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - False positive "Use of memory after it is freed" using gmock linked_ptr and std::vector."
   href="https://llvm.org/bugs/show_bug.cgi?id=28053">28053</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>False positive "Use of memory after it is freed" using gmock linked_ptr and std::vector.
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.8
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Static Analyzer
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>kremenek@apple.com
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>abascom85@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=16493" name="attach_16493" title="preprocessed file of the example code">attachment 16493</a> <a href="attachment.cgi?id=16493&action=edit" title="preprocessed file of the example code">[details]</a></span>
preprocessed file of the example code

The following warning is raised when running the analyzer on code which uses
google mocks in gmock-1.7.0 (gmock-1.7.0/include/gmock/gmock-spec-builders.h).

The clang analyzer command line I use is as follows:
tools\llvm-3.8.0\bin\clang++ --analyze -o gmock_clang_static_analysis_test.xml
-c -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-exit-time-destructors
-Wno-global-constructors -Wno-weak-vtables -std=gnu++11 -isystem
tools\mingw32-4.9.3\lib\gcc\mingw32\4.9.3\include\c++ -isystem
tools\mingw32-4.9.3\lib\gcc\mingw32\4.9.3\include\c++\mingw32 -isystem
tools\mingw32-4.9.3\lib\gcc\mingw32\4.9.3\include\c++\backward
--target=i686-pc-windows-gnu -Weverything -Wno-system-headers -Wno-date-time
-Wno-deprecated -Wno-disabled-macro-expansion
-Wno-documentation-unknown-command -Wno-missing-variable-declarations
-Wno-padded -Wno-reserved-id-macro -Wno-unused-macros -g -O0 -pipe -Werror
-nobuiltininc -nostdinc++ -isystem tools\llvm-3.8.0\lib\clang\3.8.0\include
-isystem tools\llvm-3.8.0\include -isystem
tools\mingw32-4.9.3\lib\gcc\mingw32\4.9.3\include -isystem
tools\mingw32-4.9.3\include -isystem
tools\mingw32-4.9.3\lib\gcc\mingw32\4.9.3\include-fixed -isystem
tools\mingw32-4.9.3\mingw32\include -isystem
code\c-family\third-party\gmock-1.7.0\gtest\include -isystem
code\c-family\third-party\gmock-1.7.0\include
gmock_clang_static_analysis_test.cpp

I have attached a pre-processed file.

Here is the code snippet to show both the issue and a workaround I found:

#ifndef GMOCK_CLANG_STATIC_ANALYSIS_TEST
#define GMOCK_CLANG_STATIC_ANALYSIS_TEST

#include "gmock/gmock.h"

#include <iostream>
#include <vector>

class gmock_clang_static_analyzer_test
{
    public:
        void test_clang_static_analyzer_with_gmock_linked_ptrs_false_positive()
        {
            // generates analyzer warning: Use of memory after it is freed
            std::cout << add_new_int_to_int_ptr_vector_and_return_ref();
        }

        void test_clang_static_analyzer_with_gmock_linked_ptrs_workaround()
        {
            // generates no analyzer warnings
            std::cout <<
add_new_int_to_int_ptr_vector_and_return_ref_workaround();
        }

    private:
        int &add_new_int_to_int_ptr_vector_and_return_ref()
        {
            int *raw_int_ptr = new int(123);
            testing::internal::linked_ptr<int> linked_int_ptr(raw_int_ptr);
            linked_int_ptr_vector.push_back(linked_int_ptr);
            return *raw_int_ptr;
        }

        int &add_new_int_to_int_ptr_vector_and_return_ref_workaround()
        {
            int *raw_int_ptr = new int(123);
           
linked_int_ptr_vector.push_back(testing::internal::linked_ptr<int>(raw_int_ptr));
            return *raw_int_ptr;
        }

        std::vector<testing::internal::linked_ptr<int>> linked_int_ptr_vector;
};

#endif</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>