From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1062) id DC0FD385661B; Mon, 5 Jun 2023 07:18:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DC0FD385661B Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Alan Modra To: bfd-cvs@sourceware.org Subject: [binutils-gdb] bfd_error_on_input messages X-Act-Checkin: binutils-gdb X-Git-Author: Alan Modra X-Git-Refname: refs/heads/master X-Git-Oldrev: 6fc018e9e593a3235dbf7026726ba4665373b741 X-Git-Newrev: fc998e4cb708899b5f75d8d4d2b79ed162437c41 Message-Id: <20230605071826.DC0FD385661B@sourceware.org> Date: Mon, 5 Jun 2023 07:18:26 +0000 (GMT) X-BeenThere: binutils-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 05 Jun 2023 07:18:27 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Dfc998e4cb708= 899b5f75d8d4d2b79ed162437c41 commit fc998e4cb708899b5f75d8d4d2b79ed162437c41 Author: Alan Modra Date: Mon Jun 5 16:25:16 2023 +0930 bfd_error_on_input messages =20 bfd_errmsg uses asprintf for bfd_error_on_input, which means we currently leak memory. Keep a static pointer to the message and free it in various places to minimise the leaks. bfd_set_input_error (NULL, bfd_error_no_error) is a way to free up the last string if that matters. =20 * bfd.c (input_error_msg): New static var. (bfd_set_input_error): Free it here.. (bfd_init): ..and here.. (bfd_errmsg): ..and here. Use it for asprintf output. Diff: --- bfd/bfd.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/bfd/bfd.c b/bfd/bfd.c index 71debd73733..4ae73701ce1 100644 --- a/bfd/bfd.c +++ b/bfd/bfd.c @@ -703,8 +703,9 @@ CODE_FRAGMENT */ =20 static bfd_error_type bfd_error; -static bfd *input_bfd; static bfd_error_type input_error; +static bfd *input_bfd; +static char *input_error_msg; =20 const char *const bfd_errmsgs[] =3D { @@ -792,6 +793,8 @@ bfd_set_input_error (bfd *input, bfd_error_type error_t= ag) /* This is an error that occurred during bfd_close when writing an archive, but on one of the input files. */ bfd_error =3D bfd_error_on_input; + free (input_error_msg); + input_error_msg =3D NULL; input_bfd =3D input; input_error =3D error_tag; if (input_error >=3D bfd_error_on_input) @@ -818,12 +821,13 @@ bfd_errmsg (bfd_error_type error_tag) #endif if (error_tag =3D=3D bfd_error_on_input) { - char *buf; const char *msg =3D bfd_errmsg (input_error); =20 - if (asprintf (&buf, _(bfd_errmsgs [error_tag]), + free (input_error_msg); + input_error_msg =3D NULL; + if (asprintf (&input_error_msg, _(bfd_errmsgs [error_tag]), bfd_get_filename (input_bfd), msg) !=3D -1) - return buf; + return input_error_msg; =20 /* Ick, what to do on out of memory? */ return msg; @@ -1659,6 +1663,8 @@ bfd_init (void) { bfd_error =3D bfd_error_no_error; input_bfd =3D NULL; + free (input_error_msg); + input_error_msg =3D NULL; input_error =3D bfd_error_no_error; _bfd_error_program_name =3D NULL; _bfd_error_internal =3D error_handler_fprintf;