thanks Carl
yes I saw that in my experiments
compiler think that
printf("OKAY data != NULL: %p\n",data)
is usage of data
but
if(data == NULL) is not
and therefore malloc was optimized out
I submitted bug report to clang and one of the comment mentioned very useful
thread:
https://groups.google.com/forum/#!topic/llvm-dev/lV30rcmF0ss
by malloc documentation malloc should set error code (and I’m talking about
Apple supplied documentation)
and clearly setting error code is side effect
I’d understand that code like
data = malloc(0x100000000000);
free data;
is optimized out, but if code checks data for anything or check data[n] then it
can’t be optimized out
dm
> On Jul 4, 2016, at 4:20 PM, Carl Hoefs <[email protected]> wrote:
>
>
>> On Jul 4, 2016, at 12:58 PM, Clark Cox <[email protected]> wrote:
>>
>> Malloc effectively *never* returns NULL.
>
> It does seem that malloc returns NULL on error...
>
> #include <stdlib.h>
> #include <stdio.h>
>
> int main(int argc, const char * argv[]) {
> size_t need_size = 0x1000000000000;
>
> char *data = "dummy"; // data ptr is not NULL
> data = malloc(need_size); // data ptr overwritten
>
> if(data == NULL) {
> printf("ERROR data == %p\n",data); // <----- data is NULL
> return 1;
> } else {
> printf("OKAY data != NULL: %p\n",data);
> }
> data[0] = 'c';
>
> free(data);
>
> return 0;
> }
>
> mtest(3008,0x7fff786d1300) malloc: *** mach_vm_map(size=281474976710656)
> failed (error code=3)
> *** error: can't allocate region
> *** set a breakpoint in malloc_error_break to debug
> ERROR data == 0x0
>
>
Dmitry Markman
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list ([email protected])
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/xcode-users/archive%40mail-archive.com
This email sent to [email protected]