[llvm-bugs] [Bug 28053] New: False positive "Use of memory after it is freed" using gmock linked_ptr and std::vector.

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Jun 8 10:34:15 PDT 2016


https://llvm.org/bugs/show_bug.cgi?id=28053

            Bug ID: 28053
           Summary: False positive "Use of memory after it is freed" using
                    gmock linked_ptr and std::vector.
           Product: clang
           Version: 3.8
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: normal
          Priority: P
         Component: Static Analyzer
          Assignee: kremenek at apple.com
          Reporter: abascom85 at gmail.com
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

Created attachment 16493
  --> https://llvm.org/bugs/attachment.cgi?id=16493&action=edit
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

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20160608/942a24ec/attachment-0001.html>


More information about the llvm-bugs mailing list