public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] x86: Fix a memory leak in md_assemble
@ 2024-04-09 23:01 H.J. Lu
  0 siblings, 0 replies; only message in thread
From: H.J. Lu @ 2024-04-09 23:01 UTC (permalink / raw)
  To: binutils

Fix a memory leak in md_assemble where copy may be cleared and may be
the same as copy:

      if (copy && !mnem_suffix)
        {
          line = copy;
          copy = NULL;
  no_match:

	* config/tc-i386.c (md_assemble): Properly free the xstrdup
	memory.
---
 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 8f6337b34af..502cc1b6978 100644
--- a/gas/config/tc-i386.c
+++ b/gas/config/tc-i386.c
@@ -6485,6 +6485,7 @@ md_assemble (char *line)
 {
   unsigned int j;
   char mnemonic[MAX_MNEM_SIZE], mnem_suffix = 0, *copy = NULL;
+  char *xstrdup_copy = NULL;
   const char *end, *pass1_mnem = NULL;
   enum i386_error pass1_err = 0;
   const insn_template *t;
@@ -6523,10 +6524,12 @@ md_assemble (char *line)
       return;
     }
   t = current_templates.start;
-  if (may_need_pass2 (t))
+  /* NB: LINE may be change to be the same as XSTRDUP_COPY.  */
+  if (xstrdup_copy != line && may_need_pass2 (t))
     {
       /* Make a copy of the full line in case we need to retry.  */
-      copy = xstrdup (line);
+      xstrdup_copy = xstrdup (line);
+      copy = xstrdup_copy;
     }
   line += end - line;
   mnem_suffix = i.suffix;
@@ -6535,7 +6538,7 @@ md_assemble (char *line)
   this_operand = -1;
   if (line == NULL)
     {
-      free (copy);
+      free (xstrdup_copy);
       return;
     }
 
@@ -6620,7 +6623,7 @@ md_assemble (char *line)
 	pass1_mnem = NULL;
 
   match_error:
-      free (copy);
+      free (xstrdup_copy);
 
       switch (pass1_mnem ? pass1_err : i.error)
 	{
@@ -6752,7 +6755,7 @@ md_assemble (char *line)
       return;
     }
 
-  free (copy);
+  free (xstrdup_copy);
 
   if (sse_check != check_none
       /* The opcode space check isn't strictly needed; it's there only to
-- 
2.44.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-04-09 23:01 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-09 23:01 [PATCH] x86: Fix a memory leak in md_assemble H.J. Lu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).