From f726afa492623967ebb4bb3a3212ecb2795ef7ff Mon Sep 17 00:00:00 2001 From: Christian Egli Date: Tue, 20 May 2014 16:53:02 +0200 Subject: [PATCH] Fix a memory leak When copying a string you need to hold on to the original reference so you can free it after. --- liblouis/compileTranslationTable.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/liblouis/compileTranslationTable.c b/liblouis/compileTranslationTable.c index dc6e8530e0..ad4af77864 100644 --- a/liblouis/compileTranslationTable.c +++ b/liblouis/compileTranslationTable.c @@ -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++) ; @@ -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; @@ -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; @@ -4697,7 +4703,8 @@ 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'); @@ -4705,6 +4712,7 @@ defaultTableResolver (const char *tableList, const char *base) if (!(tableFiles[k++] = resolveSubtable (subTable, base, searchPath))) { lou_logPrint ("Cannot resolve table '%s'", subTable); + free(tableList_copy); free (tableFiles); return NULL; } @@ -4713,6 +4721,7 @@ defaultTableResolver (const char *tableList, const char *base) if (last) break; } + free(tableList_copy); tableFiles[k] = NULL; return tableFiles; }