Patch 2200776 by riccardom:

this patch fix some memory and fd leaks in case of malloc() errors, and
some misc cleanup around malloc().
This commit is contained in:
yabause 2008-10-27 19:57:50 +00:00
parent 799317fa65
commit b14047b419
4 changed files with 40 additions and 20 deletions

View File

@ -315,9 +315,13 @@ static BOOL cflash_build_fat( void) {
sRomPath = szRomPath; // From MMU.cpp
files = (DIR_ENT *) malloc(MAXFILES*sizeof(DIR_ENT));
if (files == NULL)
return FALSE;
fileLink = (FILE_INFO *) malloc(MAXFILES*sizeof(FILE_INFO));
if ((files == NULL) || (fileLink == NULL))
if (fileLink == NULL) {
free(files);
return FALSE;
}
for (i=0; i<MAXFILES; i++) {
files[i].attrib = 0;
@ -341,16 +345,30 @@ static BOOL cflash_build_fat( void) {
// Allocate memory to hold information about the files
dirEntries = (DIR_ENT *) malloc(numFiles*sizeof(DIR_ENT));
dirEntryLink = (FILE_INFO *) malloc(numFiles*sizeof(FILE_INFO));
dirEntriesInCluster = (int *) malloc(NUMCLUSTERS*sizeof(int));
dirEntryPtr = (DIR_ENT **) malloc(NUMCLUSTERS*sizeof(DIR_ENT*));
if ((dirEntries==NULL) || (dirEntriesInCluster==NULL) || (dirEntryPtr==NULL))
return FALSE;
for (i=0; i<NUMCLUSTERS; i++) {
dirEntriesInCluster[i] = 0;
dirEntryPtr[i] = NULL;
if (dirEntries==NULL) {
return FALSE;
}
dirEntryLink = (FILE_INFO *) malloc(numFiles*sizeof(FILE_INFO));
if (dirEntryLink==NULL) {
free(dirEntries);
return FALSE;
}
dirEntriesInCluster = (int *) malloc(NUMCLUSTERS*sizeof(int));
if (dirEntriesInCluster==NULL) {
free(dirEntries);
free(dirEntryLink);
return FALSE;
}
dirEntryPtr = (DIR_ENT **) malloc(NUMCLUSTERS*sizeof(DIR_ENT*));
if (dirEntryPtr==NULL) {
free(dirEntries);
free(dirEntryLink);
free(dirEntriesInCluster);
return FALSE;
}
memset(dirEntriesInCluster, 0, NUMCLUSTERS*sizeof(int));
memset(dirEntryPtr, NULL, NUMCLUSTERS*sizeof(DIR_ENT*));
// Change the hierarchical layout to a flat one
for (i=0; i<=maxLevel; i++) {

View File

@ -45,10 +45,16 @@ void * FsReadFirst(const char * path, FsEntry * entry) {
return NULL;
e = readdir(tmp);
if (!e)
if (!e) {
closedir(tmp);
return NULL;
}
dir = (FsLinuxDir*)malloc(sizeof(FsLinuxDir));
if (!dir) {
closedir(tmp);
return NULL;
}
dir->dir = tmp;
strcpy(entry->cFileName, e->d_name);

View File

@ -1440,11 +1440,12 @@ gdbstub_handle_t
createStub_gdb( uint16_t port,
struct armcpu_memory_iface **cpu_memio,
struct armcpu_memory_iface *direct_memio) {
struct gdb_stub_state *stub = (gdb_stub_state*)malloc( sizeof (struct gdb_stub_state));
struct gdb_stub_state *stub;
gdbstub_handle_t handle = NULL;
int i;
int res = 0;
stub = (gdb_stub_state*)malloc( sizeof (struct gdb_stub_state));
if ( stub == NULL) {
return NULL;
}

View File

@ -158,19 +158,14 @@ void MatrixStackInit (MatrixStack *stack)
void MatrixStackSetMaxSize (MatrixStack *stack, int size)
{
int i = 0;
int i;
stack->size = size;
if (stack->matrix == NULL)
{
stack->matrix = (float*) malloc (stack->size*16*sizeof(float));
}
else
{
if (stack->matrix != NULL) {
free (stack->matrix);
stack->matrix = (float*) malloc (stack->size*16*sizeof(float));
}
stack->matrix = (float*) malloc (stack->size*16*sizeof(float));
for (i = 0; i < stack->size; i++)
{