[34/59] libctf, decl: avoid leaks of the formatted string on error

Message ID 20200630233146.338613-35-nick.alcock@oracle.com
State New
Headers show
Series
  • Deduplicating CTF linker
Related show

Commit Message

H.J. Lu via Binutils June 30, 2020, 11:31 p.m.
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/ctf-decl.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

-- 
2.27.0.247.g3dff7de930

Patch

diff --git a/libctf/ctf-decl.c b/libctf/ctf-decl.c
index 5dcf60ab08b..faf421e4765 100644
--- a/libctf/ctf-decl.c
+++ b/libctf/ctf-decl.c
@@ -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;
 }