On Feb 1, 2010, at 3:58 AM, Stuart Malin wrote:

> My code worked fine with NSString's +stringWithCString:length: even though 
> the string was not NULL terminated.  

Ah, you're right, I'd forgotten that method. It doesn't look for a NUL 
terminator because the length is given explicitly.

You still shouldn't use it though, because you have no control over the string 
encoding: it'll use the default encoding for the user's locale. This is 
MacRoman for English-language users, and various other things for non-English. 
It's practically guaranteed to give the wrong results on non-ASCII characters. 
(That's why it's deprecated.)

> When I changed that to +stringWithCString:encoding: I would *sometimes* get 
> incorrect results.

This would happen to work if the next byte past the end of the string were a 0, 
and would fail (i.e. append random garbage to the string) if it wasn't. In the 
worst case, it doesn't find a 0 byte before running off the end of allocated 
address space, and crashes with a bus error. This is a nasty source of random 
failures and crashes.

—Jens_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to