Open llvmbot opened 6 years ago
Hmm, right, std::addressof() does take the object by a non-const reference, but it certainly doesn't invalidate the object's contents.
Hi Matt,
thank you for reporting the bug. Because this comes from the Static Analyzer and not clang-tidy (as it only calls the CSA) I moved this bug to the other component.
Best Jonas
Correction: I'm using GCC 7.3.0 libstdc++
assigned to @rnkovacs
Extended Description
clang-tidy 7.0.0, using GCC 7.2.0 libstdc++
The following code:
--cut-here--
include
include
std::optional makeResponse();
void func(const char *, size_t);
void test() { auto response = makeResponse(); if (!response) return; func(response->c_str(), response->length()); } --end-cut-here
when compiled with clang-tidy 7.0.0 using a compilation like:
--cut [ { "directory": "/home/mattgodbolt/dev/clang-tidy-bug", "command": "/usr/bin/g++ -std=gnu++1z -c repro.cc -o repro.o", "file": "/home/mattgodbolt/dev/clang-tidy-bug/repro.cc" } ] --end-cut
Yields the following apparently bogus error:
~/clang-7.0.0/bin/clang-tidy repro.cc
1 warning generated. repro.cc:10:3: warning: Use of memory after it is freed [clang-analyzer-cplusplus.NewDelete] func(response->c_str(), response->length()); ^ repro.cc:9:7: note: Assuming the condition is false if (!response) return; ^ repro.cc:9:3: note: Taking false branch if (!response) return; ^ repro.cc:10:8: note: Dangling inner pointer obtained here func(response->c_str(), response->length()); ^ repro.cc:10:27: note: Calling 'optional::operator->' func(response->c_str(), response->length()); ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/optional:697:16: note: Inner pointer invalidated by call to 'addressof' { return std::addressof(this->_M_get()); } ^ repro.cc:10:27: note: Returning; memory was released func(response->c_str(), response->length()); ^ repro.cc:10:3: note: Use of memory after it is freed func(response->c_str(), response->length()); ^