[Substrate-commits] SF.net SVN: substrate: [302] trunk/Foundation/NSUnicodeString.m
Brought to you by:
landonf
|
From: <la...@us...> - 2006-09-02 05:51:56
|
Revision: 302
http://svn.sourceforge.net/substrate/?rev=302&view=rev
Author: landonf
Date: 2006-09-01 22:51:51 -0700 (Fri, 01 Sep 2006)
Log Message:
-----------
Fix a heap stomper -- ucnv_toUChars() wants the buffer length in characters, not bytes.
Modified Paths:
--------------
trunk/Foundation/NSUnicodeString.m
Modified: trunk/Foundation/NSUnicodeString.m
===================================================================
--- trunk/Foundation/NSUnicodeString.m 2006-09-02 01:33:04 UTC (rev 301)
+++ trunk/Foundation/NSUnicodeString.m 2006-09-02 05:51:51 UTC (rev 302)
@@ -99,54 +99,49 @@
- (id) initWithBytes:(const void *)bytes length:(unsigned int)length encoding:(NSStringEncoding)encoding {
UConverter *conv;
UErrorCode err = U_ZERO_ERROR;
- size_t allocSize;
self = [super init];
if (!self)
return nil;
+ /* Default to same number of characters as source bytes */
+ _length = length;
+
/* Acquire a converter, and make an educated guess as to the correct allocation */
switch (encoding) {
/* 7-bit ASCII encoding, using 8-bit characters. */
case NSASCIIStringEncoding:
conv = ucnv_open("US-ASCII", &err);
- allocSize = length * sizeof(UChar);
break;
/* 8-bit EUC encoding for Japanese. */
case NSJapaneseEUCStringEncoding:
conv = ucnv_open("EUC-JP", &err);
- allocSize = length * sizeof(UChar);
break;
/* 8-bit Unicode encoding (UTF-8). */
case NSUTF8StringEncoding:
conv = ucnv_open("UTF-8", &err);
- allocSize = length * sizeof(UChar);
break;
/* 8-bit ISO Latin 1 Encoding. */
case NSISOLatin1StringEncoding:
conv = ucnv_open("latin1", &err);
- allocSize = length * sizeof(UChar);
break;
/* 7-bit non-lossy Unicode encoding (UTF-7). */
case NSNonLossyASCIIStringEncoding:
conv = ucnv_open("UTF-7", &err);
- allocSize = length * sizeof(UChar);
break;
/* 8-bit Japanese Shift_JIS Encoding. */
case NSShiftJISStringEncoding:
conv = ucnv_open("Shift_JIS", &err);
- allocSize = length * sizeof(UChar);
break;
/* 8-bit ISO Latin 2 Encoding. */
case NSISOLatin2StringEncoding:
conv = ucnv_open("latin2", &err);
- allocSize = length * sizeof(UChar);
break;
case NSProprietaryStringEncoding: /* Fall through to UTF-16 */
@@ -169,49 +164,41 @@
conv = ucnv_open("UTF-16", &err);
break;
}
- allocSize = length;
break;
/* Microsoft Windows codepage 1251 for Cryillic characters. */
case NSWindowsCP1251StringEncoding:
conv = ucnv_open("windows-1251", &err);
- allocSize = length * sizeof(UChar);
break;
/* Microsoft Windows codepage 1252 (WinLatin1). */
case NSWindowsCP1252StringEncoding:
conv = ucnv_open("windows-1252", &err);
- allocSize = length * sizeof(UChar);
break;
/* Microsoft Windows codepage 1253, for Greek characters. */
case NSWindowsCP1253StringEncoding:
conv = ucnv_open("windows-1253", &err);
- allocSize = length * sizeof(UChar);
break;
/* Microsoft Windows codepage 1254, for Turkish characters. */
case NSWindowsCP1254StringEncoding:
conv = ucnv_open("windows-1254", &err);
- allocSize = length * sizeof(UChar);
break;
/* Microsoft Windows codepage 1250 (WinLatin2). */
case NSWindowsCP1250StringEncoding:
conv = ucnv_open("windows-1250", &err);
- allocSize = length * sizeof(UChar);
break;
/* ISO 2022 Japanese for e-mail. */
case NSISO2022JPStringEncoding:
conv = ucnv_open("ISO-2022-JP", &err);
- allocSize = length * sizeof(UChar);
break;
/* Legacy Mac Roman Encoding. */
case NSMacOSRomanStringEncoding:
conv = ucnv_open("macintosh", &err);
- allocSize = length * sizeof(UChar);
break;
/* Unhandled encodings */
@@ -241,25 +228,24 @@
}
/* Do the conversion */
- _string = NSZoneMalloc(NULL, allocSize);
+ _string = NSZoneMalloc(NULL, _length * sizeof(UChar));
while (1) {
- size_t newAllocSize;
+ unsigned int newLength;
err = U_ZERO_ERROR;
- _length = ucnv_toUChars(conv, _string, allocSize, bytes, length, &err);
- newAllocSize = _length * sizeof(UChar);
+ newLength = ucnv_toUChars(conv, _string, _length, bytes, length, &err);
if (U_SUCCESS(err)) {
/* Don't bother calling realloc for very small differences */
- if (newAllocSize != allocSize && newAllocSize - allocSize >= 5)
- _string = NSZoneRealloc(NULL, _string, newAllocSize);
- allocSize = newAllocSize;
+ if (newLength != _length && newLength - _length >= 5)
+ _string = NSZoneRealloc(NULL, _string, newLength * sizeof(UChar));
+ _length = newLength;
break;
}
if (err == U_BUFFER_OVERFLOW_ERROR) {
- allocSize = newAllocSize;
- _string = NSZoneRealloc(NULL, _string, allocSize);
+ _length = newLength;
+ _string = NSZoneRealloc(NULL, _string, _length * sizeof(UChar));
continue;
} else {
[NSException raise: NSCharacterConversionException format: @"Failure in IBM ICU's ucnv_toUChars()"];
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|