• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Where's the buffer overrun?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Where's the buffer overrun?


  • Subject: Re: Where's the buffer overrun?
  • From: Jean-Daniel Dupas <email@hidden>
  • Date: Thu, 20 Mar 2008 07:58:44 +0100


Le 20 mars 08 à 06:26, Michael Ash a écrit :

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

-bytes returns a (const void *) that you should not change.
If you want to create a raw buffer using NSData, use NSMutableData and -mutableBytes method.


_______________________________________________

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


  • Follow-Ups:
    • Re: Where's the buffer overrun?
      • From: "Michael Ash" <email@hidden>
References: 
 >Where's the buffer overrun? (From: Nick Zitzmann <email@hidden>)
 >Re: Where's the buffer overrun? (From: Chris Suter <email@hidden>)
 >Re: Where's the buffer overrun? (From: Andrew Farmer <email@hidden>)
 >Re: Where's the buffer overrun? (From: Chris Suter <email@hidden>)
 >Re: Where's the buffer overrun? (From: "Hamish Allan" <email@hidden>)
 >Re: Where's the buffer overrun? (From: Jens Alfke <email@hidden>)
 >Re: Where's the buffer overrun? (From: "Michael Ash" <email@hidden>)

  • Prev by Date: Re: Where's the buffer overrun?
  • Next by Date: Re: Correct use of NSViewController
  • Previous by thread: Re: Where's the buffer overrun?
  • Next by thread: Re: Where's the buffer overrun?
  • Index(es):
    • Date
    • Thread