ICU-5190 Test and fix some allocation issues from ucol_safeClone

X-SVN-Rev: 19632
This commit is contained in:
George Rhoten 2006-05-21 21:09:01 +00:00
parent e67193f2dc
commit 319d2629b3
2 changed files with 26 additions and 16 deletions

View file

@ -425,6 +425,7 @@ ucol_safeClone(const UCollator *coll, void *stackBuffer, int32_t * pBufferSize,
int32_t rulesPadding = 0;
uint8_t *image;
UChar *rules;
UBool allocated = FALSE;
if (status == NULL || U_FAILURE(*status)){
return 0;
@ -460,6 +461,7 @@ ucol_safeClone(const UCollator *coll, void *stackBuffer, int32_t * pBufferSize,
if (!stackBuffer || *pBufferSize < bufferSizeNeeded) {
/* allocate one here...*/
stackBufferChars = (char *)uprv_malloc(bufferSizeNeeded);
allocated = TRUE;
if (U_SUCCESS(*status)) {
*status = U_SAFECLONE_ALLOCATED_WARNING;
}
@ -495,6 +497,7 @@ ucol_safeClone(const UCollator *coll, void *stackBuffer, int32_t * pBufferSize,
localCollator->validLocale = NULL;
localCollator->rb = NULL;
localCollator->elements = NULL;
localCollator->freeOnClose = allocated;
return localCollator;
}
@ -512,6 +515,21 @@ ucol_close(UCollator *coll)
if(coll->requestedLocale != NULL) {
uprv_free(coll->requestedLocale);
}
if(coll->resCleaner != NULL) {
coll->resCleaner(coll);
}
if(coll->latinOneCEs != NULL) {
uprv_free(coll->latinOneCEs);
}
if(coll->options != NULL && coll->freeOptionsOnClose) {
uprv_free(coll->options);
}
if(coll->rules != NULL && coll->freeRulesOnClose) {
uprv_free((UChar *)coll->rules);
}
if(coll->image != NULL && coll->freeImageOnClose) {
uprv_free((UCATableHeader *)coll->image);
}
/* Here, it would be advisable to close: */
/* - UData for UCA (unless we stuff it in the root resb */
@ -520,21 +538,6 @@ ucol_close(UCollator *coll)
if(coll->freeOnClose){
/* for safeClone, if freeOnClose is FALSE,
don't free the other instance data */
if(coll->options != NULL && coll->freeOptionsOnClose) {
uprv_free(coll->options);
}
if(coll->rules != NULL && coll->freeRulesOnClose) {
uprv_free((UChar *)coll->rules);
}
if(coll->image != NULL && coll->freeImageOnClose) {
uprv_free((UCATableHeader *)coll->image);
}
if(coll->resCleaner != NULL) {
coll->resCleaner(coll);
}
if(coll->latinOneCEs != NULL) {
uprv_free(coll->latinOneCEs);
}
uprv_free(coll);
}
}

View file

@ -744,9 +744,16 @@ void TestSafeClone() {
for (index = 0; index < CLONETEST_COLLATOR_COUNT; index++)
{
ucol_setStrength(someCollators[index], UCOL_IDENTICAL);
bufferSize = 1;
err = U_ZERO_ERROR;
ucol_close(ucol_safeClone(someCollators[index], buffer[index], &bufferSize, &err));
if (err != U_SAFECLONE_ALLOCATED_WARNING) {
log_err("FAIL: collator number %d was not allocated.\n", index);
}
bufferSize = U_COL_SAFECLONE_BUFFERSIZE;
err = U_ZERO_ERROR;
ucol_setStrength(someCollators[index], UCOL_IDENTICAL);
someClonedCollators[index] = ucol_safeClone(someCollators[index], buffer[index], &bufferSize, &err);
if (someClonedCollators[index] != (UCollator *)buffer[index]) {
log_err("FAIL: Cloned collator didn't use provided buffer.\n");