Issue 84120
Summary Bad code generated with `-fdelete-null-pointer-checks`
Labels new issue
Assignees
Reporter timxx
    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)

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to