Quick fix for table resolver memory leak Make the resolver responsible for keeping the returned value in memory until the next call. see https://github.com/liblouis/liblouis/issues/315
diff --git a/liblouis/compileTranslationTable.c b/liblouis/compileTranslationTable.c index d6cfa6d..bb9e534 100644 --- a/liblouis/compileTranslationTable.c +++ b/liblouis/compileTranslationTable.c
@@ -4998,6 +4998,18 @@ return strdup(searchPath); } +/** + * Free a char** array + */ +static void +free_tablefiles(char **tables) { + char **table; + if (!tables) return; + for (table = tables; *table; table++) + free(*table); + free(tables); +} + /** * The default table resolver * @@ -5016,7 +5028,7 @@ defaultTableResolver (const char *tableList, const char *base) { char * searchPath; - char **tableFiles; + static char **tableFiles; char *subTable; char *tableList_copy; char *cp; @@ -5031,6 +5043,11 @@ for (cp = (char *)tableList; *cp != '\0'; cp++) if (*cp == ',') k++; + + // free returned value from last call + if (tableFiles) + free_tablefiles(tableFiles); + tableFiles = (char **) malloc ((k + 2) * sizeof(char *)); /* Resolve subtables */ @@ -5046,7 +5063,8 @@ logMessage (LOG_ERROR, "Cannot resolve table '%s'", subTable); free(searchPath); free(tableList_copy); - free (tableFiles); + free_tablefiles (tableFiles); + tableFiles = NULL; return NULL; } if (k == 1) @@ -5128,18 +5146,6 @@ return 0; } -/** - * Free a char** array - */ -static void -free_tablefiles(char **tables) { - char **table; - if (!tables) return; - for (table = tables; *table; table++) - free(*table); - free(tables); -} - /** * Implement include opcode *