ICU-5223 fix primary strength sortkey termination bug

X-SVN-Rev: 19794
This commit is contained in:
Vladimir Weinstein 2006-07-06 06:30:06 +00:00
parent 82d8ed3362
commit 5d24ef7fdd
2 changed files with 61 additions and 2 deletions

View File

@ -4533,7 +4533,10 @@ ucol_calcSortKey(const UCollator *coll,
}
return keyLen;
}
uint8_t *primarySafeEnd = primaries + resultLength - 2;
uint8_t *primarySafeEnd = primaries + resultLength - 1;
if(strength > UCOL_PRIMARY) {
primarySafeEnd--;
}
uint32_t minBufferSize = UCOL_MAX_BUFFER;
@ -4804,7 +4807,10 @@ ucol_calcSortKey(const UCollator *coll,
primStart = reallocateBuffer(&primaries, *result, prim, &resultLength, 2*sks, status);
if(U_SUCCESS(*status)) {
*result = primStart;
primarySafeEnd = primStart + resultLength - 2;
primarySafeEnd = primStart + resultLength - 1;
if(strength > UCOL_PRIMARY) {
primarySafeEnd--;
}
} else {
IInit_collIterate(coll, (UChar *)source, len, &s);
if(source == normSource) {

View File

@ -4686,6 +4686,58 @@ TestJ4960(void)
genericRulesStarterWithOptionsAndResult(rule, tests2, sizeof(tests2)/sizeof(tests2[0]), att2, attVals2, sizeof(att2)/sizeof(att2[0]), UCOL_EQUAL);
}
static void
TestJ5223(void)
{
char *test = "this is a test string";
UChar ustr[256];
int32_t ustr_length = u_unescape(test, ustr, 256);
unsigned char sortkey[256];
int32_t sortkey_length;
UErrorCode status = U_ZERO_ERROR;
static UCollator *coll = NULL;
coll = ucol_open("root", &status);
if(U_FAILURE(status)) {
log_err("Couldn't open UCA\n");
return;
}
ucol_setStrength(coll, UCOL_PRIMARY);
ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_PRIMARY, &status);
ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
if (U_FAILURE(status)) {
log_err("Failed setting atributes\n");
return;
}
sortkey_length = ucol_getSortKey(coll, ustr, ustr_length, NULL, 0);
if (sortkey_length > 256) return;
/* we mark the position where the null byte should be written in advance */
sortkey[sortkey_length-1] = 0xAA;
/* we set the buffer size one byte higher than needed */
sortkey_length = ucol_getSortKey(coll, ustr, ustr_length, sortkey,
sortkey_length+1);
/* no error occurs (for me) */
if (sortkey[sortkey_length-1] == 0xAA) {
log_err("Hit bug at first try\n");
}
/* we mark the position where the null byte should be written again */
sortkey[sortkey_length-1] = 0xAA;
/* this time we set the buffer size to the exact amount needed */
sortkey_length = ucol_getSortKey(coll, ustr, ustr_length, sortkey,
sortkey_length);
/* now the trailing null byte is not written */
if (sortkey[sortkey_length-1] == 0xAA) {
log_err("Hit bug at second try\n");
}
ucol_close(coll);
}
#define TEST(x) addTest(root, &x, "tscoll/cmsccoll/" # x)
void addMiscCollTest(TestNode** root)
@ -4752,6 +4804,7 @@ void addMiscCollTest(TestNode** root)
TEST(TestThaiSortKey);
TEST(TestUpperFirstQuaternary);
TEST(TestJ4960);
TEST(TestJ5223);
}
#endif /* #if !UCONFIG_NO_COLLATION */