]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
libctf, decl: avoid leaks of the formatted string on error
authorNick Alcock <nick.alcock@oracle.com>
Tue, 9 Jun 2020 09:45:07 +0000 (10:45 +0100)
committerNick Alcock <nick.alcock@oracle.com>
Wed, 22 Jul 2020 17:02:17 +0000 (18:02 +0100)
ctf_decl_sprintf builds up a formatted string in the ctf_decl_t's
cd_buf, but then on error this is hardly ever freed: we assume that
ctf_decl_fini frees it, but it leaks it instead.

Make it free it like any decent ADT should.

libctf/
* ctf-decl.c (ctf_decl_fini): Free the cd_buf.
(ctf_decl_buf): Once it escapes, don't try to free it later.

libctf/ChangeLog
libctf/ctf-decl.c

index 288ad6e554dec83a15fd8765b8a310b04533ef7c..d840bc418a133cadd1cc0a3f9c28922ed575bba0 100644 (file)
@@ -1,3 +1,8 @@
+2020-07-22  Nick Alcock  <nick.alcock@oracle.com>
+
+       * ctf-decl.c (ctf_decl_fini): Free the cd_buf.
+       (ctf_decl_buf): Once it escapes, don't try to free it later.
+
 2020-07-22  Nick Alcock  <nick.alcock@oracle.com>
 
        * ctf-types.c (ctf_type_aname): Print arg types here...
index 5dcf60ab08bdbac42b3dc459ac89fb48c4fa6905..faf421e47655c69766fd876d9d45347b0f248bc5 100644 (file)
@@ -68,6 +68,7 @@ ctf_decl_fini (ctf_decl_t *cd)
          free (cdp);
        }
     }
+  free (cd->cd_buf);
 }
 
 void
@@ -195,5 +196,7 @@ void ctf_decl_sprintf (ctf_decl_t *cd, const char *format, ...)
 
 char *ctf_decl_buf (ctf_decl_t *cd)
 {
-  return cd->cd_buf;
+  char *buf = cd->cd_buf;
+  cd->cd_buf = NULL;
+  return buf;
 }