Skip to content

Commit

Permalink
Fixed memory leak in collectFilePaths and do not allocate a found pat…
Browse files Browse the repository at this point in the history
…h buffer until the first is located.
  • Loading branch information
danij-deng committed Apr 4, 2011
1 parent f08458c commit 444add5
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions doomsday/engine/portable/src/sys_filein.c
Expand Up @@ -965,9 +965,9 @@ static int C_DECL compareFoundEntryByName(const void* a, const void* b)
/// Collect a list of paths including those which have been mapped.
static foundentry_t* collectFilePaths(const ddstring_t* searchPath, int* retCount)
{
int count = 0, max = 16;
foundentry_t* found = malloc(max * sizeof(*found));
ddstring_t wildPath, origWildPath;
foundentry_t* found = NULL;
int count = 0, max = 0;
finddata_t fd;

Str_Init(&origWildPath);
Expand All @@ -992,7 +992,10 @@ static foundentry_t* collectFilePaths(const ddstring_t* searchPath, int* retCoun
{
if(count >= max)
{
max *= 2;
if(0 == max)
max = 16;
else
max *= 2;
found = realloc(found, sizeof(*found) * max);
}
memset(&found[count], 0, sizeof(*found));
Expand All @@ -1005,9 +1008,16 @@ static foundentry_t* collectFilePaths(const ddstring_t* searchPath, int* retCoun
myfindend(&fd);
}}

Str_Free(&origWildPath);
Str_Free(&wildPath);

if(retCount)
*retCount = count;
return count > 0? found : 0;
if(0 != count)
return found;

free(found);
return NULL;
}

static int iterateFilePaths(const ddstring_t* pattern, const ddstring_t* searchPath,
Expand All @@ -1019,7 +1029,7 @@ static int iterateFilePaths(const ddstring_t* pattern, const ddstring_t* searchP
int result = 0, count;
foundentry_t* found;

if(0 != (found = collectFilePaths(searchPath, &count)))
if(NULL != (found = collectFilePaths(searchPath, &count)))
{
ddstring_t path, localPattern;

Expand Down

0 comments on commit 444add5

Please sign in to comment.