public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@polymtl.ca>
To: gdb-patches@sourceware.org
Subject: [PATCH] sim/m32c: fix memory leaks in opc2c
Date: Mon,  5 Apr 2021 10:58:56 -0400	[thread overview]
Message-ID: <20210405145856.3925296-1-simon.marchi@polymtl.ca> (raw)

When building with AddressSanitizer, sim/m32c fails with:

./opc2c -l r8c.out /home/simark/src/binutils-gdb/sim/m32c/r8c.opc > r8c.c
sim_log: r8c.out

=================================================================
==3919390==ERROR: LeakSanitizer: detected memory leaks

    Direct leak of 485 byte(s) in 131 object(s) allocated from:
        #0 0x7ffff761fa69 in __interceptor_strdup /build/gcc/src/gcc/libsanitizer/asan/asan_interceptors.cpp:452
        #1 0x555555557abe in dump_lines /home/simark/src/binutils-gdb/sim/m32c/opc2c.c:243
        #2 0x555555558bc9 in emit_indirect /home/simark/src/binutils-gdb/sim/m32c/opc2c.c:382
        #3 0x555555558c66 in emit_indirect /home/simark/src/binutils-gdb/sim/m32c/opc2c.c:387
        #4 0x55555555c463 in main /home/simark/src/binutils-gdb/sim/m32c/opc2c.c:718
        #5 0x7ffff741fb24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)

    Direct leak of 360 byte(s) in 100 object(s) allocated from:
        #0 0x7ffff761fa69 in __interceptor_strdup /build/gcc/src/gcc/libsanitizer/asan/asan_interceptors.cpp:452
        #1 0x555555557abe in dump_lines /home/simark/src/binutils-gdb/sim/m32c/opc2c.c:243
        #2 0x555555558bc9 in emit_indirect /home/simark/src/binutils-gdb/sim/m32c/opc2c.c:382
        #3 0x55555555c463 in main /home/simark/src/binutils-gdb/sim/m32c/opc2c.c:718
        #4 0x7ffff741fb24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)

    Direct leak of 4 byte(s) in 1 object(s) allocated from:
        #0 0x7ffff7677459 in __interceptor_malloc /build/gcc/src/gcc/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x55555555b3df in main /home/simark/src/binutils-gdb/sim/m32c/opc2c.c:658
        #2 0x7ffff741fb24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)

Fix the leak in dump_lines by free-ing the elements of varnames.

Fix the leak in main by removing the vlist variable, which seems unused.

sim/m32c/ChangeLog:

	* opc2c.c (dump_lines): Free elements of varnames.
	(main): Remove vlist variable.

Change-Id: I5a92fd229da623bd32841b083c54c5c868455e91
---
 sim/m32c/opc2c.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/sim/m32c/opc2c.c b/sim/m32c/opc2c.c
index 082d4e9b8a42..7d1604f4fb3a 100644
--- a/sim/m32c/opc2c.c
+++ b/sim/m32c/opc2c.c
@@ -286,6 +286,9 @@ dump_lines (opcode * op, int level, Indirect * ind)
     printf ("%*s%s", level, "", op->lines[i]);
   if (op->comment)
     printf ("%*s}\n", level, "");
+
+  for (i = 0; i < vn; i++)
+    free (varnames[i]);
 }
 
 void
@@ -507,7 +510,6 @@ main (int argc, char **argv)
   FILE *in;
   int lineno = 0;
   int i;
-  VaryRef *vlist;
 
   if (argc > 2 && strcmp (argv[1], "-l") == 0)
     {
@@ -655,8 +657,6 @@ main (int argc, char **argv)
 
   qsort (opcodes, n_opcodes, sizeof (opcodes[0]), op_cmp);
 
-  vlist = (VaryRef *) malloc (n_varies * sizeof (VaryRef));
-
   for (i = 0; i < n_opcodes; i++)
     {
       int j, b, v;
-- 
2.30.1


             reply	other threads:[~2021-04-05 14:58 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-05 14:58 Simon Marchi [this message]
2021-04-05 16:23 ` Mike Frysinger
2021-04-05 18:46   ` Simon Marchi
2021-04-05 21:51     ` Mike Frysinger
2021-04-06  1:36       ` Simon Marchi
2021-04-06 10:41         ` Mike Frysinger
2021-04-06 13:28           ` Simon Marchi
2021-04-06 13:45             ` Tom Tromey
2021-04-06 18:01               ` Philippe Waroquiers
2021-04-06 22:45             ` Mike Frysinger
2021-04-07  1:45               ` Simon Marchi
2021-04-07 11:38                 ` Mike Frysinger
2021-04-07 14:19                   ` Simon Marchi
2021-04-08  4:51                     ` Mike Frysinger
2021-04-08 13:52                       ` Simon Marchi
2021-04-08  4:50 ` Mike Frysinger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210405145856.3925296-1-simon.marchi@polymtl.ca \
    --to=simon.marchi@polymtl.ca \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).