Free allocated memory in case of errors.

Thanks to Christoph Fritz for the patch.
This commit is contained in:
Thomas Huth 2010-03-21 16:04:31 +01:00
parent 8938e40155
commit 2b7d1c543b
3 changed files with 31 additions and 0 deletions

View File

@ -386,6 +386,7 @@ char* SDLGui_FileSelect(const char *path_and_name, char **zip_path, bool bAllowN
if (!(File_DirExists(path) || getcwd(path, FILENAME_MAX)))
{
perror("SDLGui_FileSelect: non-existing path and CWD failed");
free(pStringMem);
return NULL;
}
}
@ -409,6 +410,12 @@ char* SDLGui_FileSelect(const char *path_and_name, char **zip_path, bool bAllowN
if (browsingzip)
{
files = ZIP_GetFilesDir(zipfiles, zipdir, &entries);
if(!files)
{
fprintf(stderr, "SDLGui_FileSelect: ZIP_GetFilesDir error!\n");
free(pStringMem);
return NULL;
}
}
else
{

View File

@ -187,6 +187,21 @@ void ZIP_FreeZipDir(zip_dir *f_zd)
}
/*-----------------------------------------------------------------------*/
/**
* Free the memory that has been allocated for fentries.
*/
static void ZIP_FreeFentries(struct dirent **fentries, int entries)
{
while (entries > 0)
{
entries--;
free(fentries[entries]);
}
free((struct dirent **)entries);
}
/*-----------------------------------------------------------------------*/
/**
* Returns a list of files from the directory (dir) in a zip file list (zip)
@ -221,7 +236,10 @@ struct dirent **ZIP_GetFilesDir(const zip_dir *zip, const char *dir, int *entrie
files->nfiles = 1;
temp = (char *)malloc(4);
if (!temp)
{
ZIP_FreeZipDir(files);
return NULL;
}
temp[0] = temp[1] = '.';
temp[2] = '/';
temp[3] = '\0';
@ -252,6 +270,7 @@ struct dirent **ZIP_GetFilesDir(const zip_dir *zip, const char *dir, int *entrie
if (!files->names[files->nfiles])
{
perror("ZIP_GetFilesDir");
ZIP_FreeZipDir(files);
return NULL;
}
strncpy(files->names[files->nfiles], temp, slash+1);
@ -266,6 +285,7 @@ struct dirent **ZIP_GetFilesDir(const zip_dir *zip, const char *dir, int *entrie
if (!files->names[files->nfiles])
{
perror("ZIP_GetFilesDir");
ZIP_FreeZipDir(files);
return NULL;
}
strncpy(files->names[files->nfiles], temp, strlen(temp));
@ -292,6 +312,7 @@ struct dirent **ZIP_GetFilesDir(const zip_dir *zip, const char *dir, int *entrie
if (!fentries[i])
{
perror("ZIP_GetFilesDir");
ZIP_FreeFentries(fentries, i+1);
return NULL;
}
strcpy(fentries[i]->d_name, files->names[i]);
@ -440,6 +461,7 @@ static void *ZIP_ExtractFile(unzFile uf, const char *filename, uLong size)
if (err != UNZ_OK)
{
Log_Printf(LOG_ERROR, "ZIP_ExtractFile: could not open file\n");
free(buf);
return NULL;
}

View File

@ -91,6 +91,7 @@ int main(int argc, char *argv[])
if (!diskBuffer || nImageBytes < 512*8)
{
fprintf(stderr,"Could not read MSA disk!\n");
free(targetFileName);
return -1;
}
@ -113,6 +114,7 @@ int main(int argc, char *argv[])
if (!diskBuffer || nImageBytes < 512*8)
{
fprintf(stderr,"Could not read ST disk!\n");
free(targetFileName);
return -1;
}