From ad9f07e66fded8a44adba15d576960cef587f9d4 Mon Sep 17 00:00:00 2001 From: Jun-ichi Takimoto Date: Wed, 1 Aug 2018 21:36:44 +0900 Subject: 43227: fix memory leaks in term{cap,info}.c --- Src/Modules/termcap.c | 14 ++++---------- Src/Modules/terminfo.c | 14 ++++---------- Src/utils.c | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 20 deletions(-) (limited to 'Src') diff --git a/Src/Modules/termcap.c b/Src/Modules/termcap.c index 60a6e138a..af4009a3a 100644 --- a/Src/Modules/termcap.c +++ b/Src/Modules/termcap.c @@ -345,16 +345,7 @@ int boot_(UNUSED(Module m)) { #ifdef HAVE_TGETENT -# ifdef HAVE_SETUPTERM - int errret; - - /* - * Just because we can't set up the terminal doesn't - * mean the modules hasn't booted---TERM may change, - * and it should be handled dynamically---so ignore errors here. - */ - (void)setupterm((char *)0, 1, &errret); -# endif + zsetupterm(); #endif return 0; } @@ -363,6 +354,9 @@ boot_(UNUSED(Module m)) int cleanup_(Module m) { +#ifdef HAVE_TGETENT + zdeleteterm(); +#endif return setfeatureenables(m, &module_features, NULL); } diff --git a/Src/Modules/terminfo.c b/Src/Modules/terminfo.c index bbd325899..4596b41d2 100644 --- a/Src/Modules/terminfo.c +++ b/Src/Modules/terminfo.c @@ -338,16 +338,7 @@ int boot_(UNUSED(Module m)) { #ifdef USE_TERMINFO_MODULE -# ifdef HAVE_SETUPTERM - int errret; - - /* - * Just because we can't set up the terminal doesn't - * mean the modules hasn't booted---TERM may change, - * and it should be handled dynamically---so ignore errors here. - */ - (void)setupterm((char *)0, 1, &errret); -# endif + zsetupterm(); #endif return 0; @@ -357,6 +348,9 @@ boot_(UNUSED(Module m)) int cleanup_(Module m) { +#ifdef USE_TERMINFO_MODULE + zdeleteterm(); +#endif return setfeatureenables(m, &module_features, NULL); } diff --git a/Src/utils.c b/Src/utils.c index ee2ad207f..075d27241 100644 --- a/Src/utils.c +++ b/Src/utils.c @@ -375,6 +375,43 @@ zerrmsg(FILE *file, const char *fmt, va_list ap) fflush(file); } +/* + * Wrapper for setupterm() and del_curterm(). + * These are called from terminfo.c and termcap.c. + */ +static int term_count; /* reference count of cur_term */ + +/**/ +mod_export void +zsetupterm(void) +{ +#ifdef HAVE_SETUPTERM + int errret; + + DPUTS(term_count < 0 || (term_count > 0 && !cur_term), + "inconsistent term_count and/or cur_term"); + /* + * Just because we can't set up the terminal doesn't + * mean the modules hasn't booted---TERM may change, + * and it should be handled dynamically---so ignore errors here. + */ + if (term_count++ == 0) + (void)setupterm((char *)0, 1, &errret); +#endif +} + +/**/ +mod_export void +zdeleteterm(void) +{ +#ifdef HAVE_SETUPTERM + DPUTS(term_count < 1 || !cur_term, + "inconsistent term_count and/or cur_term"); + if (--term_count == 0) + del_curterm(cur_term); +#endif +} + /* Output a single character, for the termcap routines. * * This is used instead of putchar since it can be a macro. */ -- cgit 1.4.1