[llvm-bugs] [Bug 35760] New: [Clang-optimization] wrong code for the optimization of static global variable with malloc.

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Dec 27 07:13:05 PST 2017


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

            Bug ID: 35760
           Summary: [Clang-optimization] wrong code for the optimization
                    of static global variable with malloc.
           Product: clang
           Version: 4.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: yoshimatsu2000 at gmail.com
                CC: llvm-bugs at lists.llvm.org

#include <stdio.h>
#include <stdlib.h>

static int* g_i = NULL;

static void PutsSomething() {
    if (g_i == NULL) {
        g_i = (int*)malloc(sizeof(*g_i));
        puts("0"); // -O0
    } else {
        puts("1"); // -O1, -O2
    }
}

int main(void) {
    g_i = NULL;
    PutsSomething();
    // free(g_i); 
    return EXIT_SUCCESS;
}

$ clang++-4.0 -O0 test.cpp; ./a.out
0
$ clang++-4.0 -O1 test.cpp; ./a.out
1
$ clang++-4.0 -O2 test.cpp; ./a.out
1

The output is different while the optimization is enabled.
When I cancel comment for free(g_i), this behavior is not seen.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20171227/13ed3f51/attachment.html>


More information about the llvm-bugs mailing list