Re: Where's the buffer overrun?
Re: Where's the buffer overrun?
- Subject: Re: Where's the buffer overrun?
- From: "Michael Ash" <email@hidden>
- Date: Thu, 20 Mar 2008 01:26:44 -0400
On Thu, Mar 20, 2008 at 12:56 AM, Jens Alfke <email@hidden> wrote:
> It's never a good idea to
> make assumptions about where a Foundation object is putting its data;
> if you need to access the current bytes of an NSData, call -bytes on it.
This was a little confusing to me until I thought about it a bit, so I
thought I would emphasize this.
When you use these methods and tell it to free the memory, the object
you create takes ownership of the block of memory. This means that
it's responsible for freeing it, but more importantly it means that
it's *in control* of freeing it. In other words, conceptually speaking
it's never safe to refer back to this block of memory directly after
you create the object. Instead you should query the object to get
whatever you need from it, in this case query the -bytes method to get
the pointer back.
If I understand the API contract, the code in question is not even
safe when using NSData. It happens to work, but the contract does not
guarantee it. To be safe, the NSData should be created, then the
return value from its -bytes method should be returned. If I'm wrong
about this, I'd appreciate any corrections.
Mike
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden