[PATCH 03/10] libsframe: fix memory leak in sframe_decode

Indu Bhagat <[email protected]>
Newsgroups gmane.comp.gnu.binutils
Message-ID <[email protected]>
From: Indu Bhagat <[email protected]>

If dctx is successfully allocated by malloc, but the subsequent malloc
for tempbuf fails, sframe_decode () immediately returns NULL without
freeing dctx.  This leaks the newly allocated sframe_decoder_ctx
structure.

Change the return on tempbuf == NULL to jump to decode_fail_free to
address this.
---
 libsframe/sframe.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/libsframe/sframe.c b/libsframe/sframe.c
index 712dd0fdb68..d50a9e5689b 100644
--- a/libsframe/sframe.c
+++ b/libsframe/sframe.c
@@ -1448,7 +1448,10 @@ sframe_decode (const char *sf_buf, size_t sf_size, int *errp)
       /* Allocate a new buffer and initialize it.  */
       char *tempbuf = malloc (sf_size * sizeof (char));
       if (tempbuf == NULL)
-	return sframe_ret_set_errno (errp, SFRAME_ERR_NOMEM);
+	{
+	  sframe_ret_set_errno (errp, SFRAME_ERR_NOMEM);
+	  goto decode_fail_free;
+	}
       memcpy (tempbuf, sf_buf, sf_size);
 
       /* Flip the header first.  */
-- 
2.43.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.