Hi all

in the Xcode 7.3.1 the following simple code

#include <iostream>

int main(int argc, const char * argv[]) {
    size_t need_size = 0x1000000000000;

    void *data = malloc(need_size);

    if(data == NULL) {
        std::cout << "data == NULL" << std::endl;
        return 1;
    } else {
        std::cout << "data != NULL" << std::endl;
    }
    free(data);
    
    return 0;
}

reports for the release build

data != NULL
Program ended with exit code: 0


which is clearly wrong, because right answer would be 

data == NULL
Program ended with exit code: 1


g++ behaves correctly

recently I found out that we reported that problem to Apple 

and we got an answer

""Engineering has determined that this issue behaves as intended based on the 
following information: 

The compiler "knows" how malloc works, and is allowed to optimize as if it 
never fails. 

We are now closing this bug report.” 

clearly in my situation clang knows nothing :((

clang can’t optimize malloc here, because result of the malloc is used.

we have much more complex use case where 

malloc was optimized out,

I just simplified the code

Note: -fno-builtin flag solve the problem


also if I use 

        std::cout << "data != NULL" << data << std::endl;

malloc wasn’t optimized



Dmitry Markman


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (Xcode-users@lists.apple.com)
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/xcode-users/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to