Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to call Block_copy() and Block_release() on nil?

Do I need to check or does it behave like free() and such where it will ignore nil?

I'm unable to find documentation that states clearly one way or the other.

My suspicion is that it is safe. E.g., this...

(lldb) p (void)_Block_release(0)
<no result>

...didn't crash, it seems. I think that's what that means?

like image 408
i_am_jorf Avatar asked Jan 29 '26 19:01

i_am_jorf


1 Answers

I have tried this:

Block_copy(-1);

This obviously caused a crash, so I stepped into the assembly code:

0x7fff9035342a:  movq   %rdi, %r15
0x7fff9035342d:  xorl   %eax, %eax
0x7fff9035342f:  testq  %r15, %r15
0x7fff90353432:  je     0x7fff903535ca            ; _Block_copy_internal + 430

In rdi there should be the only argument pushed: the block address in this case.

It's content is stored in r15 and it's being compared before the conditional jump. And the address 0x7fff903535ca is at the end, when it starts to pop from the stack, so yes, it's nil safe:

0x7fff903535ca:  addq   $8, %rsp
0x7fff903535ce:  popq   %rbx
0x7fff903535cf:  popq   %r12
0x7fff903535d1:  popq   %r13
0x7fff903535d3:  popq   %r14
0x7fff903535d5:  popq   %r15
0x7fff903535d7:  popq   %rbp
0x7fff903535d8:  ret    
like image 159
Ramy Al Zuhouri Avatar answered Feb 02 '26 15:02

Ramy Al Zuhouri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!