ICU-11774 toolutil Package use uprv_free & LocalMemory, not free()

X-SVN-Rev: 37883
This commit is contained in:
Markus Scherer 2015-09-04 18:59:17 +00:00
parent b4b5fc8392
commit 0bdc5dd937

View File

@ -1,7 +1,7 @@
/*
*******************************************************************************
*
* Copyright (C) 1999-2014, International Business Machines
* Copyright (C) 1999-2015, International Business Machines
* Corporation and others. All Rights Reserved.
*
*******************************************************************************
@ -304,7 +304,6 @@ static uint8_t *
readFile(const char *path, const char *name, int32_t &length, char &type) {
char filename[1024];
FILE *file;
uint8_t *data;
UErrorCode errorCode;
int32_t fileLength, typeEnum;
@ -327,34 +326,32 @@ readFile(const char *path, const char *name, int32_t &length, char &type) {
/* allocate the buffer, pad to multiple of 16 */
length=(fileLength+0xf)&~0xf;
data=(uint8_t *)uprv_malloc(length);
if(data==NULL) {
icu::LocalMemory<uint8_t> data((uint8_t *)uprv_malloc(length));
if(data.isNull()) {
fclose(file);
fprintf(stderr, "icupkg: malloc error allocating %d bytes.\n", (int)length);
exit(U_MEMORY_ALLOCATION_ERROR);
}
/* read the file */
if(fileLength!=(int32_t)fread(data, 1, fileLength, file)) {
if(fileLength!=(int32_t)fread(data.getAlias(), 1, fileLength, file)) {
fprintf(stderr, "icupkg: error reading \"%s\"\n", filename);
fclose(file);
free(data);
exit(U_FILE_ACCESS_ERROR);
}
/* pad the file to a multiple of 16 using the usual padding byte */
if(fileLength<length) {
memset(data+fileLength, 0xaa, length-fileLength);
memset(data.getAlias()+fileLength, 0xaa, length-fileLength);
}
fclose(file);
// minimum check for ICU-format data
errorCode=U_ZERO_ERROR;
typeEnum=getTypeEnumForInputData(data, length, &errorCode);
typeEnum=getTypeEnumForInputData(data.getAlias(), length, &errorCode);
if(typeEnum<0 || U_FAILURE(errorCode)) {
fprintf(stderr, "icupkg: not an ICU data file: \"%s\"\n", filename);
free(data);
#if !UCONFIG_NO_LEGACY_CONVERSION
exit(U_INVALID_FORMAT_ERROR);
#else
@ -364,7 +361,7 @@ readFile(const char *path, const char *name, int32_t &length, char &type) {
}
type=makeTypeLetter(typeEnum);
return data;
return data.orphan();
}
// .dat package file representation ---------------------------------------- ***
@ -421,11 +418,11 @@ Package::Package()
Package::~Package() {
int32_t idx;
free(inData);
uprv_free(inData);
for(idx=0; idx<itemCount; ++idx) {
if(items[idx].isDataOwned) {
free(items[idx].data);
uprv_free(items[idx].data);
}
}
@ -1050,7 +1047,7 @@ Package::addItem(const char *name, uint8_t *data, int32_t length, UBool isDataOw
} else {
// same-name item found, replace it
if(items[idx].isDataOwned) {
free(items[idx].data);
uprv_free(items[idx].data);
}
// keep the item's name since it is the same
@ -1089,7 +1086,7 @@ Package::removeItem(int32_t idx) {
if(idx>=0) {
// remove the item
if(items[idx].isDataOwned) {
free(items[idx].data);
uprv_free(items[idx].data);
}
// move the following items up