Skip to content

malloc is optimized out for release build #28790

Closed
@llvmbot

Description

@llvmbot
Bugzilla Link 28416
Resolution INVALID
Resolved on May 26, 2021 07:02
Version 3.8
OS MacOS X
Reporter LLVM Bugzilla Contributor
CC @majnemer,@DougGregor,@hfinkel,@yaupon0

Extended Description

the following code behaves incorrectly

int main(int argc, const char * argv[]) {
size_t need_size = 0x1000000000000;
void *data = malloc(need_size);
if(data == NULL) {
printf("data == NULL\n");
return 1;
} else {
printf("data != NULL\n");
}
free(data);
return 0;
}

it returns

data != NULL
Program ended with exit code: 0

but it should return

data == NULL
Program ended with exit code: 1

gcc handles that situation correctly

clang shouldn't optimize it out, because result of the malloc is used and

caller of my program relies on the exit code

Note, clang flag -fno-builtin prevents clang from malloc optimize out

and solve the problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillac++invalidResolved as invalid, i.e. not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions