Re: Reading NSInputStream
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