[v2,18/21] libcc1: fix a memory leak

Message ID 20210428010119.806184-19-tom@tromey.com
State New
Headers show
Series
  • C++11-based improvements for libcc1
Related show

Commit Message

Tom Tromey April 28, 2021, 1:01 a.m.
libcc1 has a memory leak when calling fork_exec -- it allocates a new
vector of arguments, but then does not free it anywhere.  This patch
changes this code to use std::vector instead.

Note that the previous code tried to avoid bad_alloc.  I don't believe
this is very important.  For one thing, plenty of other allocations do
not bother with this.

libcc1/ChangeLog
2021-04-27  Tom Tromey  <tom@tromey.com>

	* gdbctx.hh (do_compile): Use std::vector.
---
 libcc1/ChangeLog | 4 ++++
 libcc1/gdbctx.hh | 8 ++------
 2 files changed, 6 insertions(+), 6 deletions(-)

-- 
2.26.2

Comments

Andrew Pinski via Gcc-patches April 28, 2021, 4:07 p.m. | #1
On 4/27/2021 7:01 PM, Tom Tromey wrote:
> libcc1 has a memory leak when calling fork_exec -- it allocates a new

> vector of arguments, but then does not free it anywhere.  This patch

> changes this code to use std::vector instead.

>

> Note that the previous code tried to avoid bad_alloc.  I don't believe

> this is very important.  For one thing, plenty of other allocations do

> not bother with this.

>

> libcc1/ChangeLog

> 2021-04-27  Tom Tromey  <tom@tromey.com>

>

> 	* gdbctx.hh (do_compile): Use std::vector.


OK

jeff

Patch

diff --git a/libcc1/gdbctx.hh b/libcc1/gdbctx.hh
index 4a48381f2b4a..4d2488344bc8 100644
--- a/libcc1/gdbctx.hh
+++ b/libcc1/gdbctx.hh
@@ -308,15 +308,11 @@  namespace cc1_plugin
 
       self->add_callbacks ();
 
-      char **argv = new (std::nothrow) char *[self->args.size () + 1];
-      if (argv == NULL)
-	return 0;
-
+      std::vector<char *> argv (self->args.size () + 1);
       for (unsigned int i = 0; i < self->args.size (); ++i)
 	argv[i] = const_cast<char *> (self->args[i].c_str ());
-      argv[self->args.size ()] = NULL;
 
-      return self->fork_exec (argv, fds, stderr_fds);
+      return self->fork_exec (argv.data (), fds, stderr_fds);
     }
 
     static int