ICU-6132 Add code to check for memory allocation error and memory leaks in udata.c, uloc.c, and ufile.c.

X-SVN-Rev: 23252
This commit is contained in:
Michael Ow 2008-01-16 22:24:05 +00:00
parent cd54ac79e7
commit 5e72fa288d
3 changed files with 52 additions and 21 deletions

View File

@ -1,7 +1,7 @@
/*
******************************************************************************
*
* Copyright (C) 1999-2006, International Business Machines
* Copyright (C) 1999-2008, International Business Machines
* Corporation and others. All Rights Reserved.
*
******************************************************************************
@ -216,6 +216,10 @@ static UHashtable *udata_getHashTable() {
}
tHT = uhash_open(uhash_hashChars, uhash_compareChars, NULL, &err);
/* Check for null pointer. */
if (tHT == NULL) {
return NULL; /* TODO: Handle this error better. */
}
uhash_setValueDeleter(tHT, DataCacheElement_deleter);
umtx_lock(NULL);
@ -477,6 +481,10 @@ static void udata_pathiter_init(UDataPathIterator *iter, const char *path, const
} else {
if(uprv_strlen(pkg) + 2 > U_DATA_PATHITER_BUFSIZ) {
iter->packageStub = uprv_malloc(uprv_strlen(pkg)+2);
/* Check for null pointer. */
if (iter->packageStub == NULL) {
return;
}
} else {
iter->packageStub = iter->packageStubBuf;
}

View File

@ -1487,6 +1487,11 @@ uloc_openKeywordList(const char *keywordList, int32_t keywordListSize, UErrorCod
return NULL;
}
result = (UEnumeration *)uprv_malloc(sizeof(UEnumeration));
/* Null pointer test */
if (result == NULL) {
*status = U_MEMORY_ALLOCATION_ERROR;
return NULL;
}
uprv_memcpy(result, &gKeywordsEnum, sizeof(UEnumeration));
myContext = uprv_malloc(sizeof(UKeywordsContext));
if (myContext == NULL) {
@ -2681,25 +2686,27 @@ static void _load_installedLocales()
if(U_SUCCESS(status)) {
localeCount = ures_getSize(&installed);
temp = (char **) uprv_malloc(sizeof(char*) * (localeCount+1));
ures_resetIterator(&installed);
while(ures_hasNext(&installed)) {
ures_getNextString(&installed, NULL, (const char **)&temp[i++], &status);
/* Check for null pointer */
if (temp != NULL) {
ures_resetIterator(&installed);
while(ures_hasNext(&installed)) {
ures_getNextString(&installed, NULL, (const char **)&temp[i++], &status);
}
temp[i] = NULL;
umtx_lock(NULL);
if (_installedLocales == NULL)
{
_installedLocales = temp;
_installedLocalesCount = localeCount;
temp = NULL;
ucln_common_registerCleanup(UCLN_COMMON_ULOC, uloc_cleanup);
}
umtx_unlock(NULL);
uprv_free(temp);
ures_close(&installed);
}
temp[i] = NULL;
umtx_lock(NULL);
if (_installedLocales == NULL)
{
_installedLocales = temp;
_installedLocalesCount = localeCount;
temp = NULL;
ucln_common_registerCleanup(UCLN_COMMON_ULOC, uloc_cleanup);
}
umtx_unlock(NULL);
uprv_free(temp);
ures_close(&installed);
}
ures_close(index);
}
@ -2885,7 +2892,13 @@ uloc_acceptLanguageFromHTTP(char *result, int32_t resultAvailable, UAcceptResult
/* eat spaces prior to semi */
for(t=(paramEnd-1);(paramEnd>s)&&isspace(*t);t--)
;
j[n].locale = uprv_strndup(s,(int32_t)((t+1)-s));
/* Check for null pointer from uprv_strndup */
char *tempstr = uprv_strndup(s,(int32_t)((t+1)-s));
if (tempstr == NULL) {
*status = U_MEMORY_ALLOCATION_ERROR;
return -1;
}
j[n].locale = tempstr;
uloc_canonicalize(j[n].locale,tmp,sizeof(tmp)/sizeof(tmp[0]),status);
if(strcmp(j[n].locale,tmp)) {
uprv_free(j[n].locale);
@ -2932,6 +2945,12 @@ uloc_acceptLanguageFromHTTP(char *result, int32_t resultAvailable, UAcceptResult
return -1;
}
strs = uprv_malloc((size_t)(sizeof(strs[0])*n));
/* Check for null pointer */
if (strs == NULL) {
uprv_free(j); /* Free to avoid memory leak */
*status = U_MEMORY_ALLOCATION_ERROR;
return;
}
for(i=0;i<n;i++) {
#if defined(ULOC_DEBUG)
/*fprintf(stderr,"%d: s <%s> q <%g>\n", i, j[i].locale, j[i].q);*/

View File

@ -1,7 +1,7 @@
/*
******************************************************************************
*
* Copyright (C) 1998-2006, International Business Machines
* Copyright (C) 1998-2008, International Business Machines
* Corporation and others. All Rights Reserved.
*
******************************************************************************
@ -144,6 +144,10 @@ u_fstropen(UChar *stringBuf,
}
result = (UFILE*) uprv_malloc(sizeof(UFILE));
/* Null pointer test */
if (result == NULL) {
return NULL; /* Just get out. */
}
uprv_memset(result, 0, sizeof(UFILE));
result->str.fBuffer = stringBuf;
result->str.fPos = stringBuf;