1 Index: /trunk/icu4c/source/common/utext.cpp
2 ===================================================================
3 --- icu.orig/source/common/utext.cpp
4 +++ icu/source/common/utext.cpp
8 -// Must be less than 85, because of byte mapping from UChar indexes to native indexes.
9 -// Worst case is three native bytes to one UChar. (Supplemenaries are 4 native bytes
11 +// Must be less than 42 (256/6), because of byte mapping from UChar indexes to native indexes.
12 +// Worst case there are six UTF-8 bytes per UChar.
13 +// obsolete 6 byte form fd + 5 trails maps to fffd
14 +// obsolete 5 byte form fc + 4 trails maps to fffd
15 +// non-shortest 4 byte forms maps to fffd
16 +// normal supplementaries map to a pair of utf-16, two utf8 bytes per utf-16 unit
17 +// mapToUChars array size must allow for the worst case, 6.
18 +// This could be brought down to 4, by treating fd and fc as pure illegal,
19 +// rather than obsolete lead bytes. But that is not compatible with the utf-8 access macros.
21 enum { UTF8_TEXT_CHUNK_SIZE=32 };
23 // one for a supplementary starting in the last normal position,
24 // and one for an entry for the buffer limit position.
25 - uint8_t mapToUChars[UTF8_TEXT_CHUNK_SIZE*3+6]; // Map native offset from bufNativeStart to
26 + uint8_t mapToUChars[UTF8_TEXT_CHUNK_SIZE*6+6]; // Map native offset from bufNativeStart to
27 // correspoding offset in filled part of buf.
30 u8b = (UTF8Buf *)ut->p; // the current buffer
31 mapIndex = ix - u8b->toUCharsMapStart;
32 + U_ASSERT(mapIndex < (int32_t)sizeof(UTF8Buf::mapToUChars));
33 ut->chunkOffset = u8b->mapToUChars[mapIndex] - u8b->bufStartIdx;
36 // If index is at the end, there is no character there to look at.
38 + // Note: this function will only move the index back if it is on a trail byte
39 + // and there is a preceding lead byte and the sequence from the lead
40 + // through this trail could be part of a valid UTF-8 sequence
41 + // Otherwise the index remains unchanged.
42 U8_SET_CP_START(s8, 0, ix);
45 uint8_t *mapToNative = u8b->mapToNative;
46 uint8_t *mapToUChars = u8b->mapToUChars;
47 - int32_t toUCharsMapStart = ix - (UTF8_TEXT_CHUNK_SIZE*3 + 1);
48 + int32_t toUCharsMapStart = ix - sizeof(UTF8Buf::mapToUChars) + 1;
49 + // Note that toUCharsMapStart can be negative. Happens when the remaining
50 + // text from current position to the beginning is less than the buffer size.
51 + // + 1 because mapToUChars must have a slot at the end for the bufNativeLimit entry.
52 int32_t destIx = UTF8_TEXT_CHUNK_SIZE+2; // Start in the overflow region
53 // at end of buffer to leave room
55 // Special case ASCII range for speed.
56 buf[destIx] = (UChar)c;
57 + U_ASSERT(toUCharsMapStart <= srcIx);
58 mapToUChars[srcIx - toUCharsMapStart] = (uint8_t)destIx;
59 mapToNative[destIx] = (uint8_t)(srcIx - toUCharsMapStart);
61 mapToUChars[sIx-- - toUCharsMapStart] = (uint8_t)destIx;
62 } while (sIx >= srcIx);
63 + U_ASSERT(toUCharsMapStart <= (srcIx+1));
65 // Set native indexing limit to be the current position.
67 U_ASSERT(index<=ut->chunkNativeLimit);
68 int32_t mapIndex = index - u8b->toUCharsMapStart;
69 + U_ASSERT(mapIndex < (int32_t)sizeof(UTF8Buf::mapToUChars));
70 int32_t offset = u8b->mapToUChars[mapIndex] - u8b->bufStartIdx;
71 U_ASSERT(offset>=0 && offset<=ut->chunkLength);
72 Index: /trunk/icu4c/source/test/intltest/utxttest.cpp
73 ===================================================================
74 --- icu.orig/source/test/intltest/utxttest.cpp
75 +++ icu/source/test/intltest/utxttest.cpp
77 case 7: name = "Ticket12130";
78 if (exec) Ticket12130(); break;
79 + case 8: name = "Ticket12888";
80 + if (exec) Ticket12888(); break;
81 default: name = ""; break;
83 @@ -1584,2 +1586,62 @@
87 +// Ticket 12888: bad handling of illegal utf-8 containing many instances of the archaic, now illegal,
88 +// six byte utf-8 forms. Original implementation had an assumption that
89 +// there would be at most three utf-8 bytes per UTF-16 code unit.
90 +// The five and six byte sequences map to a single replacement character.
92 +void UTextTest::Ticket12888() {
93 + const char *badString =
94 + "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
95 + "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
96 + "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
97 + "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
98 + "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
99 + "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
100 + "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
101 + "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
102 + "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
103 + "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
104 + "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
105 + "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
106 + "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
107 + "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
108 + "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
109 + "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
110 + "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
111 + "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
112 + "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
113 + "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80";
115 + UErrorCode status = U_ZERO_ERROR;
116 + LocalUTextPointer ut(utext_openUTF8(NULL, badString, -1, &status));
117 + TEST_SUCCESS(status);
119 + UChar32 c = utext_next32(ut.getAlias());
120 + if (c == U_SENTINEL) {
124 + int32_t endIdx = utext_getNativeIndex(ut.getAlias());
125 + if (endIdx != (int32_t)strlen(badString)) {
126 + errln("%s:%d expected=%d, actual=%d", __FILE__, __LINE__, strlen(badString), endIdx);
130 + for (int32_t prevIndex = endIdx; prevIndex>0;) {
131 + UChar32 c = utext_previous32(ut.getAlias());
132 + int32_t currentIndex = utext_getNativeIndex(ut.getAlias());
134 + errln("%s:%d (expected, actual, index) = (%d, %d, %d)\n",
135 + __FILE__, __LINE__, 0xfffd, c, currentIndex);
138 + if (currentIndex != prevIndex - 6) {
139 + errln("%s:%d: wrong index. Expected, actual = %d, %d",
140 + __FILE__, __LINE__, prevIndex - 6, currentIndex);
143 + prevIndex = currentIndex;
146 Index: /trunk/icu4c/source/test/intltest/utxttest.h
147 ===================================================================
148 --- icu.orig/source/test/intltest/utxttest.h
149 +++ icu/source/test/intltest/utxttest.h
153 + void Ticket12888();