[18/19] libctf: dump: fix small leak

Message ID 20240730153707.168357-19-nick.alcock@oracle.com
State New
Headers
Series libctf: various bugfixes (including a write into freed memory), and loosen constraints on enums |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_binutils_check--master-arm success Test passed

Commit Message

Nick Alcock July 30, 2024, 3:37 p.m. UTC
  If you asprintf something and then use it only as input to another asprintf,
it helps to free it afterwards.

libctf/
	* ctf-dump.c (ctf_dump_header): Free the flagstr after use.
	(ctf_dump): Make a NULL return slightly clearer.
---
 libctf/ctf-dump.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
  

Patch

diff --git a/libctf/ctf-dump.c b/libctf/ctf-dump.c
index cd41996084d..a37d4b75d45 100644
--- a/libctf/ctf-dump.c
+++ b/libctf/ctf-dump.c
@@ -349,6 +349,7 @@  ctf_dump_header (ctf_dict_t *fp, ctf_dump_state_t *state)
 
       if (asprintf (&str, "Flags: 0x%x (%s)", fp->ctf_openflags, flagstr) < 0)
 	goto err;
+      free (flagstr);
       ctf_dump_append (state, str);
     }
 
@@ -814,7 +815,7 @@  ctf_dump (ctf_dict_t *fp, ctf_dump_state_t **statep, ctf_sect_names_t sect,
       if (!str)
 	{
 	  ctf_set_errno (fp, ENOMEM);
-	  return str;
+	  return NULL;
 	}
     }