From: Shlomi Fish Date: Tue, 16 Jan 2018 09:29:14 +0000 (+0200) Subject: Fix some memory leaks. X-Git-Tag: fortune-mod-2.2.0~6 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0c74581bc1c3ea56f5828cd637291138d3586b5a;p=fortune-mod Fix some memory leaks. "valgrind doesn't lie.". See https://github.com/shlomif/fortune-mod/issues/22#issuecomment-357811116 . Now the valgrind test passes with the new Recode at https://github.com/rrthomas/Recode . --- diff --git a/fortune-mod/fortune/fortune.c b/fortune-mod/fortune/fortune.c index b2a7e1e..9872917 100644 --- a/fortune-mod/fortune/fortune.c +++ b/fortune-mod/fortune/fortune.c @@ -1680,6 +1680,26 @@ static int max(register int i, register int j) return (i >= j ? i : j); } +static void free_desc(FILEDESC *ptr) +{ + while (ptr) + { + free_desc(ptr->child); + do_free(ptr->datfile); + do_free(ptr->posfile); + do_free(ptr->name); + do_free(ptr->path); + if (ptr->inf) + { + fclose(ptr->inf); + ptr->inf = NULL; + } + FILEDESC *next = ptr->next; + free(ptr); + ptr = next; + } +} + int main(int ac, char *av[]) { const char *ctype; @@ -1742,6 +1762,9 @@ int main(int ac, char *av[]) recode_delete_request(request); recode_delete_outer(outer); + + /* Free the File_list */ + free_desc(File_list); exit(0); /* NOTREACHED */ } diff --git a/fortune-mod/tests/t/valgrind.t b/fortune-mod/tests/t/valgrind.t index e7b976b..59912f3 100644 --- a/fortune-mod/tests/t/valgrind.t +++ b/fortune-mod/tests/t/valgrind.t @@ -6,8 +6,8 @@ use warnings; use Test::More; use Test::RunValgrind; -plan skip_all => 'lib-recode has memory leaks'; -# plan tests => 1; +# plan skip_all => 'lib-recode has memory leaks'; +plan tests => 1; # TEST Test::RunValgrind->new({})->run(