Skip to content

Commit

Permalink
Fix a memory leak
Browse files Browse the repository at this point in the history
When copying a string you need to hold on to the original reference so
you can free it after.
  • Loading branch information
egli committed May 20, 2014
1 parent 23079f3 commit f726afa
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions liblouis/compileTranslationTable.c
Expand Up @@ -4627,7 +4627,8 @@ resolveSubtable (const char *table, const char *base, const char *searchPath)
char *dir;
int last;
char *cp;
for (dir = strdup (searchPath + 1); ; dir = cp + 1)
char *searchPath_copy = strdup (searchPath + 1);
for (dir = searchPath_copy; ; dir = cp + 1)
{
for (cp = dir; *cp != '\0' && *cp != ','; cp++)
;
Expand All @@ -4636,11 +4637,15 @@ resolveSubtable (const char *table, const char *base, const char *searchPath)
if (dir == cp)
dir = ".";
sprintf (tableFile, "%s%c%s", dir, DIR_SEP, table);
if (stat (tableFile, &info) == 0 && !(info.st_mode & S_IFDIR))
return tableFile;
if (stat (tableFile, &info) == 0 && !(info.st_mode & S_IFDIR))
{
free(searchPath_copy);
return tableFile;
}
if (last)
break;
}
free(searchPath_copy);
}
free (tableFile);
return NULL;
Expand All @@ -4666,6 +4671,7 @@ defaultTableResolver (const char *tableList, const char *base)
char searchPath[MAXSTRING];
char **tableFiles;
char *subTable;
char *tableList_copy;
char *cp;
char *path;
int last;
Expand Down Expand Up @@ -4697,14 +4703,16 @@ defaultTableResolver (const char *tableList, const char *base)

/* Resolve subtables */
k = 0;
for (subTable = strdup (tableList); ; subTable = cp + 1)
tableList_copy = strdup (tableList);
for (subTable = tableList_copy; ; subTable = cp + 1)
{
for (cp = subTable; *cp != '\0' && *cp != ','; cp++);
last = (*cp == '\0');
*cp = '\0';
if (!(tableFiles[k++] = resolveSubtable (subTable, base, searchPath)))
{
lou_logPrint ("Cannot resolve table '%s'", subTable);
free(tableList_copy);
free (tableFiles);
return NULL;
}
Expand All @@ -4713,6 +4721,7 @@ defaultTableResolver (const char *tableList, const char *base)
if (last)
break;
}
free(tableList_copy);
tableFiles[k] = NULL;
return tableFiles;
}
Expand Down

0 comments on commit f726afa

Please sign in to comment.