<html>
<head>
<base href="https://bugs.llvm.org/">
</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 - CallAndMessage false positive for "T* const member" while it's not nullptr"
href="https://bugs.llvm.org/show_bug.cgi?id=44492">44492</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>CallAndMessage false positive for "T* const member" while it's not nullptr
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>unspecified
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>All
</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>dcoughlin@apple.com
</td>
</tr>
<tr>
<th>Reporter</th>
<td>zinovy.nis@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>dcoughlin@apple.com, llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre>The following code emits false-positive
--------------------------------------------------------
#include <cassert>
#include <string>
class C {
public:
int foo(int x) { return x * 2; }
};
class A {
public:
A(C* c, std::string s) : c_(c), s_(s) {
assert(c_);
bar("12345");
c_->foo(42);
}
void bar(std::string s) {
// s_, not c_!
// c_ is not changed here.
s_ = s;
}
private:
std::string s_; // [1]
C* const c_ = nullptr; // [2]
};
int main(int argc, char** /*unused*/) {
C* c = new C;
A a(c, "123");
return 1;
};
--------------------------------------------------------
<span class="quote">> clang-tidy.exe -checks=-*,*CallAndMessage* test.cc</span >
...
test.cc:14:5: warning: Called C++ object pointer is null
[clang-analyzer-core.CallAndMessage]
c_->foo(42);
^
test.cc:29:5: note: Calling constructor for 'A'
A a(c, "123");
^
test.cc:12:12: note: Field 'c_' is non-null
assert(c_);
^
test.cc:12:12: note: Field 'c_' is non-null
test.cc:12:5: note: Left side of '||' is true
assert(c_);
^
C:\Program Files (x86)\Windows
Kits\10\Include\10.0.18362.0\ucrt\assert.h:33:30: note: expanded from macro
'assert'
(!!(expression)) ||
\
^
test.cc:13:5: note: Calling 'A::bar'
bar("12345");
^
test.cc:19:5: note: Null pointer value stored to 'a.c_'
s_ = s;
^
test.cc:13:5: note: Returning from 'A::bar'
bar("12345");
^
test.cc:14:5: note: Called C++ object pointer is null
c_->foo(42);
^
"c_" is considered nullptr, while it's not changed during a call to "bar".
Moreover, 1) if we swap [1] and [2] lines, then CallAndMessage is not raised;
2) if we remove 'const' in [2] then CallAndMessage is not raised.</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>