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

            Bug ID: 39572
           Summary: Optimizer removes unrelated test due to later
                    static_cast
           Product: clang
           Version: 7.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangb...@nondot.org
          Reporter: scott.sm...@purestorage.com
                CC: llvm-bugs@lists.llvm.org, richard-l...@metafoo.co.uk

Created attachment 21096
  --> https://bugs.llvm.org/attachment.cgi?id=21096&action=edit
Complete test case

For the following code (see attachments for the full test case):

        for (size_t i = 0; i < 20; i++) {
                op * res = nullptr;
                if (rand() % 2) {   // compiler turns this into: if (rand(),
true) {                
                        count++;
                        res = get_an_op();
                }
                static_cast<no_dispose *>(res)->execute(); // nullptr
dereference 50% of the time   
        }
        printf("ASDF count = %d\n", count);

I expect the code to usually crash due to dereference of nullptr.  However when
compiled with -O3, it prints "ASDF count = 20".

Note the static_cast - if you remove it, the code crashes as you'd expect. 
This may be a case of the compiler making unrelated decisions due the
unexpected behavior of the null dereference (though the cast itself is legal,
and the change in behavior only happens with the cast).  However it does seem
odd to me that the cast affects the earlier if() statement.

To repeat:

good:
$ clang++-7 -o good a.cpp
$ ./good
Segmentation fault (core dumped)

bad:
$ clang++-7 -o bad -O3 a.cpp
$ ./bad
ASDF count = 20

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

Reply via email to