<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/84120>84120</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Bad code generated with `-fdelete-null-pointer-checks`
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
timxx
</td>
</tr>
</table>
<pre>
demo.cpp
```cpp
#include <iostream>
#include <string>
class Foo {
public:
Foo() : crash_me("string") {
}
int safeGet() {
if (this) {
return 0;
} else {
return 1;
}
}
void destroy() {
delete this;
}
public:
std::string crash_me;
};
int main(int argc, char **argv) {
Foo *f = nullptr;
if (argc == 2) {
f = new Foo();
}
std::cout << f << "\n";
std::cout << f->safeGet() << "\n";
f->destroy();
std::cout << "everything is ok\n";
return 0;
}
```
compile the above source code with `-O3` (or any other optimize flags such as `-Os`)
`clang++ demo.cpp -O3 -o demo`
then run: `./demo`
and it output wrong result and crashed at the end
```
0
0
Segmentation fault (core dumped)
```
`safeGet` should be returns `1` if `this` is nullptr. And `destroy` should not crash the program.
It works if disable the optimze (use `-O0` flag), or just disable the delete null pointer checks (compile with `-fno-delete-null-pointer-checks`).
Please note, it works for clang 9.0 version (and msvc too, but g++ seems also has the problem)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx8Vdmu4ygQ_RryUkqEcdYHP2TpjOapR5oPGGFTsenGYAHO7dtfPypsZ7k3fa3IC0WdWs4pIkPQtUUs2OrAVqeZ7GPjfBF1--vXrHTqvVDYukXVdYyfGN-P9zUffvd1kWtbmV4hsPyoXYgeZcvyb6_MIXpt67sx3SsjQ4Czc8A2h2Gp60ujK5aPOwCA7ExsmdgBy_dQeRma_1pMS2KEFSKZJxAYr_sX25weA9OSthGCvOBfGCf4m7--ABPb2OjwEpcuj7H3FjjLPwbdnABNwAe3cW_2au-XOV6dVqAwRO_ePyU5XgoNRoSU7CP-B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