From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1062) id E90FB3858D32; Sun, 19 Feb 2023 03:46:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E90FB3858D32 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] Avoid memory leak in chew X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: cd414f90c340145d811259d10e84b126d73777d0 X-Git-Newrev: 85fc5801281d38897b652b298d35074b7b73ed3a Message-Id: <20230219034613.E90FB3858D32@sourceware.org> Date: Sun, 19 Feb 2023 03:46:13 +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: Sun, 19 Feb 2023 03:46:14 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D85fc5801281d= 38897b652b298d35074b7b73ed3a commit 85fc5801281d38897b652b298d35074b7b73ed3a Author: Tom Tromey Date: Wed Feb 15 16:09:35 2023 -0700 Avoid memory leak in chew =20 An earlier patch of mine introduced a memory leak in chew. The bug was that the new "variable" word didn't free the following word. This patch fixes it by arranging to transfer ownership of the name to the variable itself. =20 * doc/chew.c (add_variable): New function, from add_intrinsic_variable. (add_intrinsic_variable): Call add_variable. (compile): Call add_variable. Diff: --- bfd/doc/chew.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/bfd/doc/chew.c b/bfd/doc/chew.c index 19e3781bdda..cd399697abd 100644 --- a/bfd/doc/chew.c +++ b/bfd/doc/chew.c @@ -1241,9 +1241,9 @@ add_intrinsic (char *name, void (*func) (void)) } =20 static void -add_intrinsic_variable (char *name, intptr_t *loc) +add_variable (char *name, intptr_t *loc) { - dict_type *new_d =3D newentry (xstrdup (name)); + dict_type *new_d =3D newentry (name); pcu p =3D { push_variable }; add_to_definition (new_d, p); p.l =3D (intptr_t) loc; @@ -1252,6 +1252,12 @@ add_intrinsic_variable (char *name, intptr_t *loc) add_to_definition (new_d, p); } =20 +static void +add_intrinsic_variable (const char *name, intptr_t *loc) +{ + add_variable (xstrdup (name), loc); +} + void compile (char *string) { @@ -1333,7 +1339,7 @@ compile (char *string) continue; intptr_t *loc =3D xmalloc (sizeof (intptr_t)); *loc =3D 0; - add_intrinsic_variable (word, loc); + add_variable (word, loc); string =3D nextword (string, &word); } else