public inbox for binutils-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] PR29255, memory leak in make_tempdir
@ 2022-06-17 11:59 Alan Modra
  0 siblings, 0 replies; only message in thread
From: Alan Modra @ 2022-06-17 11:59 UTC (permalink / raw)
  To: bfd-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=d6e1d48c83b165c129cb0aa78905f7ca80a1f682

commit d6e1d48c83b165c129cb0aa78905f7ca80a1f682
Author: Alan Modra <amodra@gmail.com>
Date:   Fri Jun 17 09:13:38 2022 +0930

    PR29255, memory leak in make_tempdir
    
            PR 29255
            * bucomm.c (make_tempdir, make_tempname): Free template on all
            failure paths.

Diff:
---
 binutils/bucomm.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/binutils/bucomm.c b/binutils/bucomm.c
index fdc2209df9c..4395cb9f7f5 100644
--- a/binutils/bucomm.c
+++ b/binutils/bucomm.c
@@ -537,8 +537,9 @@ make_tempname (const char *filename, int *ofd)
 #else
   tmpname = mktemp (tmpname);
   if (tmpname == NULL)
-    return NULL;
-  fd = open (tmpname, O_RDWR | O_CREAT | O_EXCL, 0600);
+    fd = -1;
+  else
+    fd = open (tmpname, O_RDWR | O_CREAT | O_EXCL, 0600);
 #endif
   if (fd == -1)
     {
@@ -556,22 +557,23 @@ char *
 make_tempdir (const char *filename)
 {
   char *tmpname = template_in_dir (filename);
+  char *ret;
 
 #ifdef HAVE_MKDTEMP
-  return mkdtemp (tmpname);
+  ret = mkdtemp (tmpname);
 #else
-  tmpname = mktemp (tmpname);
-  if (tmpname == NULL)
-    return NULL;
+  ret = mktemp (tmpname);
 #if defined (_WIN32) && !defined (__CYGWIN32__)
   if (mkdir (tmpname) != 0)
-    return NULL;
+    ret = NULL;
 #else
   if (mkdir (tmpname, 0700) != 0)
-    return NULL;
+    ret = NULL;
 #endif
-  return tmpname;
 #endif
+  if (ret == NULL)
+    free (tmpname);
+  return ret;
 }
 
 /* Parse a string into a VMA, with a fatal error if it can't be


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

only message in thread, other threads:[~2022-06-17 11:59 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-17 11:59 [binutils-gdb] PR29255, memory leak in make_tempdir Alan Modra

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).