From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1039) id 6CD4C3858D35; Tue, 16 Apr 2024 13:34:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6CD4C3858D35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1713274484; bh=qNSVaYvV4wi5nQra/6iunX/fkOlfciWxRSHbszcsjxs=; h=From:To:Subject:Date:From; b=lQWG4tiMkbO4sJ1wKGffukegDs2O65xXFtp2vFQM0Ar8oRJHn2HXoWt4f8ZEEowz7 3w741NB9f2MDCb2/9F9lJsLpvxMWF2YiSWfzarfNfBNlJ0rkoaR97Bo1nFkWHq8GEN vmBSgQcrh+faVRdw56MIIs5r8oC40ZOfXMpZ/wgQ= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: H.J. Lu To: binutils-cvs@sourceware.org Subject: [binutils-gdb] x86: Fix a memory leak in md_assemble X-Act-Checkin: binutils-gdb X-Git-Author: H.J. Lu X-Git-Refname: refs/heads/master X-Git-Oldrev: aa38e605634e5ce8357d610655d6eb72c3e7d2d2 X-Git-Newrev: 2d4c39a885d4d12325d0a9be9e014e75a295fb25 Message-Id: <20240416133444.6CD4C3858D35@sourceware.org> Date: Tue, 16 Apr 2024 13:34:44 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D2d4c39a885d4= d12325d0a9be9e014e75a295fb25 commit 2d4c39a885d4d12325d0a9be9e014e75a295fb25 Author: H.J. Lu Date: Tue Apr 9 16:01:16 2024 -0700 x86: Fix a memory leak in md_assemble =20 Fix a memory leak in md_assemble where copy may be cleared and may be the same as copy: =20 if (copy && !mnem_suffix) { line =3D copy; copy =3D NULL; no_match: =20 * config/tc-i386.c (md_assemble): Properly free the xstrdup memory. Diff: --- gas/config/tc-i386.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/gas/config/tc-i386.c b/gas/config/tc-i386.c index 56b2431b1bc..1637f55759c 100644 --- a/gas/config/tc-i386.c +++ b/gas/config/tc-i386.c @@ -6515,6 +6515,7 @@ md_assemble (char *line) { unsigned int j; char mnemonic[MAX_MNEM_SIZE], mnem_suffix =3D 0, *copy =3D NULL; + char *xstrdup_copy =3D NULL; const char *end, *pass1_mnem =3D NULL; enum i386_error pass1_err =3D 0; const insn_template *t; @@ -6553,10 +6554,12 @@ md_assemble (char *line) return; } t =3D current_templates.start; - if (may_need_pass2 (t)) + /* NB: LINE may be change to be the same as XSTRDUP_COPY. */ + if (xstrdup_copy !=3D line && may_need_pass2 (t)) { /* Make a copy of the full line in case we need to retry. */ - copy =3D xstrdup (line); + xstrdup_copy =3D xstrdup (line); + copy =3D xstrdup_copy; } line +=3D end - line; mnem_suffix =3D i.suffix; @@ -6565,7 +6568,7 @@ md_assemble (char *line) this_operand =3D -1; if (line =3D=3D NULL) { - free (copy); + free (xstrdup_copy); return; } =20 @@ -6650,7 +6653,7 @@ md_assemble (char *line) pass1_mnem =3D NULL; =20 match_error: - free (copy); + free (xstrdup_copy); =20 switch (pass1_mnem ? pass1_err : i.error) { @@ -6782,7 +6785,7 @@ md_assemble (char *line) return; } =20 - free (copy); + free (xstrdup_copy); =20 if (sse_check !=3D check_none /* The opcode space check isn't strictly needed; it's there only to