• 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: Reading NSInputStream
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Reading NSInputStream


  • Subject: Re: Reading NSInputStream
  • From: Greg Guerin <email@hidden>
  • Date: Wed, 21 Oct 2009 17:17:18 -0700

Ashley Perrien wrote:

uint8_t *readBuffer;

At this point, readBuffer is an uninitialized local variable.

NSUInteger bufferLength;
BOOL gotBuffer = [readStream getBuffer: &readBuffer length: &bufferLength];

This use of readBuffer is safe, because it's passed by reference, and the caller expects it to be filled in. However, it will only be filled in if YES is returned.


int len = [readStream read: readBuffer maxLength: 300];

This use of readBuffer is unsafe. If readBuffer is not initialized before this point, then you are reading using a garbage pointer. If the getBuffer:length: method was invoked first, and returned YES, then you may have a buffer overrun instead of a garbage pointer dereference. If it was invoked and returned NO, then readBuffer may contain anything.


Or maybe readBuffer is safe, because you neglected to post the code that initializes it to point to an actual memory buffer of suitable length.

Really can't tell, because you're only posting isolated code fragments, instead of complete and self-contained fragments that show the entire context.


[readStream read: [returnMessage mutableBytes] maxLength: 300];

You're neglecting the return value, which indicates the actual number of bytes read. You're probably lucking out that the mutableBytes have been zeroed, so any data less than 300 bytes ends up with a terminating nul character.


  -- GG

_______________________________________________

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


  • Prev by Date: Re: correctly Controlling Garbage Collection
  • Next by Date: Re: NSColor for darker hightlight color
  • Previous by thread: Re: Reading NSInputStream
  • Next by thread: NSURLDownload and file quarantine
  • Index(es):
    • Date
    • Thread