From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14139 invoked by alias); 31 Jul 2018 14:31:11 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: List-Unsubscribe: X-Seq: 43227 Received: (qmail 14530 invoked by uid 1010); 31 Jul 2018 14:31:11 -0000 X-Qmail-Scanner-Diagnostics: from rcpt-mqugw.biglobe.ne.jp by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(133.208.100.3):SA:0(-2.6/5.0):. Processed in 1.318708 secs); 31 Jul 2018 14:31:11 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham autolearn_force=no version=3.4.1 X-Envelope-From: takimoto-j@kba.biglobe.ne.jp X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | X-Biglobe-Sender: Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 10.3 \(3273\)) Subject: Re: [PATCH] fix several memory leaks From: "Jun T." X-Priority: 3 In-Reply-To: <825056567.96193.1532958460017@mail2.virginmedia.com> Date: Tue, 31 Jul 2018 23:30:33 +0900 Content-Transfer-Encoding: quoted-printable Message-Id: References: <8CE35533-3221-4A9D-94D9-07B4DEF54C80@kba.biglobe.ne.jp> <825056567.96193.1532958460017@mail2.virginmedia.com> To: zsh-workers@zsh.org X-Mailer: Apple Mail (2.3273) X-Biglobe-Spnum: 53267 > 2018/07/30 22:47, Peter Stephenson = wrote: >=20 > If setupterm(), over which we have no control, is really a problem we = probably > need a front-end with a lock to make sure it only ever gets called = once. >=20 > Something like a function in utils.c which as the #ifdef buried in it > and simply does nothing if we don't HAVE_SETUPTERM, for example? Ok, it seems we need a reference count of cur_term. How about this? 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); } =20 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 =20 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); } =20 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); } =20 +/* + * 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++ =3D=3D 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 =3D=3D 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. */ =20