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

            Bug ID: 44712
           Summary: False positive coming from EXPECT_NEAR in googletest
                    (aka gtest).
           Product: clang
           Version: 9.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Static Analyzer
          Assignee: dcough...@apple.com
          Reporter: cassio.n...@gmail.com
                CC: dcough...@apple.com, llvm-bugs@lists.llvm.org

The static analyzer issues two false positives inside googletest in this code.

    #include <gtest/gtest.h>

    TEST(Foo, Test) {
        EXPECT_NEAR(1.0, 1.1, 0.2);
    }

$ scan-build clang++ -std=c++17 -g -c file.cpp

/usr/include/gtest/internal/gtest-port.h:1205:20: warning: Use of memory after
it is freed
  T* get() const { return ptr_; }
                   ^~~~~~~~~~~
/usr/include/gtest/internal/gtest-port.h:1216:16: warning: Attempt to delete
released memory
        delete ptr_;
               ^
The following short self-contained example reproduces the issue. (For easy of
reference, it uses the same names that appear inside googletest's code.)

    template <typename T>
    struct scoped_ptr {
        ~scoped_ptr() { delete ptr_; }
        T* ptr_;
    };

    struct AssertionResult {
        operator bool() const { return success_; }
        char* message() const { return message_.ptr_; }
        bool success_;
        scoped_ptr<char> message_;
    };

    AssertionResult DoubleNearPredFormat();

    void partial_expansion_of_EXPECT_NEAR() {
        if (const AssertionResult gtest_ar = (DoubleNearPredFormat()))
            gtest_ar.message();
    }

FWIW: Any of the following changes make the problem to go away:

1) Removing the extra parentheses around the call to DoubleNearPredFormat().
(This is very surprising to me. Indeed, the need to add those parentheses to
reproduce the issue was quite hard to find!)

2) Replacing scoped_ptr with std::unique_ptr. (Although not identical, these
classes are similar.)

3) Making scoped_ptr a non template and substituting T by char.

I've seen this behavior with clang 7.1 and 9.0.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to