Re: EXC_BAD_ACCESS in NSData
Re: EXC_BAD_ACCESS in NSData
- Subject: Re: EXC_BAD_ACCESS in NSData
- From: Jens Alfke <email@hidden>
- Date: Mon, 26 May 2014 17:13:48 -0700
On May 26, 2014, at 4:20 PM, Graham Cox <email@hidden> wrote:
> That's just as wrong - you are using the first few bytes of the data as the length, which it certainly isn't (except for possibly a very few special cases that just so happen to have the length as the first field of the data).
No, it’s extremely common to have a data format where a variable-length field is prefixed with its length. That looks like what this code is trying to read.
The right way to do this would be something like:
uint32_t length = *(uint32_t*)bytes;
length = CFSwapInt32BigToHost(length);
You need to be explicit about the size of integer in use (“int” has different sizes on different platforms) and you need to convert from the byte-order used in the file format (which is almost always big-endian.)
—Jens
_______________________________________________
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