public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Indu Bhagat <indu.bhagat@oracle.com>
To: binutils@sourceware.org
Cc: Indu Bhagat <indu.bhagat@oracle.com>
Subject: [PATCH 1/2] libsframe: fix a memory leak in sframe_decode
Date: Thu, 22 Dec 2022 14:54:56 -0800	[thread overview]
Message-ID: <20221222225457.1095930-2-indu.bhagat@oracle.com> (raw)
In-Reply-To: <20221222225457.1095930-1-indu.bhagat@oracle.com>

sframe_decode () needs to malloc a temporary buffer of the same size as
the input buffer (containing the SFrame section bytes) when endian
flipping is needed.  The decoder keeps the endian flipped contents in
this buffer for its usage.  This code is necessary when the target
endianneess is not the same as host endianness.

The malloc'd buffer needs to be kept track of, so that it can freed up in
sframe_decoder_free () later.

ChangeLog:

	* libsframe/sframe-impl.h (struct sframe_decoder_ctx): Add new
	member to keep track of the internally malloc'd buffer.
	* libsframe/sframe.c (sframe_decoder_free): Free it up.
	(sframe_decode): Update the reference to the buffer.
---
 libsframe/sframe-impl.h | 15 +++++++++++----
 libsframe/sframe.c      |  9 +++++++++
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/libsframe/sframe-impl.h b/libsframe/sframe-impl.h
index 0e61c977886..340d3b35042 100644
--- a/libsframe/sframe-impl.h
+++ b/libsframe/sframe-impl.h
@@ -32,10 +32,17 @@ extern "C"
 
 struct sframe_decoder_ctx
 {
-  sframe_header sfd_header;	      /* SFrame header.  */
-  uint32_t *sfd_funcdesc;	      /* SFrame function desc entries table.  */
-  void *sfd_fres;		      /* SFrame FRE table.  */
-  int sfd_fre_nbytes;		      /* Number of bytes needed for SFrame FREs.  */
+  /* SFrame header.  */
+  sframe_header sfd_header;
+  /* SFrame function desc entries table.  */
+  uint32_t *sfd_funcdesc;
+  /* SFrame FRE table.  */
+  void *sfd_fres;
+  /* Number of bytes needed for SFrame FREs.  */
+  int sfd_fre_nbytes;
+  /* Reference to the internally malloc'd buffer, if any, for endian flipping
+     the original input buffer before decoding.  */
+  void *sfd_buf;
 };
 
 struct sframe_encoder_ctx
diff --git a/libsframe/sframe.c b/libsframe/sframe.c
index b8fde2f04f8..e41c95b9944 100644
--- a/libsframe/sframe.c
+++ b/libsframe/sframe.c
@@ -548,6 +548,11 @@ sframe_decoder_free (sframe_decoder_ctx **decoder)
 	  free (dctx->sfd_fres);
 	  dctx->sfd_fres = NULL;
 	}
+      if (dctx->sfd_buf != NULL)
+	{
+	  free (dctx->sfd_buf);
+	  dctx->sfd_buf = NULL;
+	}
 
       free (*decoder);
       *decoder = NULL;
@@ -824,6 +829,10 @@ sframe_decode (const char *sf_buf, size_t sf_size, int *errp)
 	  return sframe_ret_set_errno (errp, SFRAME_ERR_BUF_INVAL);
 	}
       frame_buf = tempbuf;
+      /* This buffer is malloc'd when endian flipping the contents of the input
+	 buffer are needed.  Keep a reference to it so it can be free'd up
+	 later in sframe_decoder_free ().  */
+      dctx->sfd_buf = tempbuf;
     }
   else
     frame_buf = (char *)sf_buf;
-- 
2.37.2


  reply	other threads:[~2022-12-22 22:55 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-22 22:54 [PATCH 0/2] libsframe: fix some memory leaks Indu Bhagat
2022-12-22 22:54 ` Indu Bhagat [this message]
2022-12-22 22:54 ` [PATCH 2/2] libsframe: testsuite: fix memory leaks in testcases Indu Bhagat
2022-12-23  9:55 ` [PATCH 0/2] libsframe: fix some memory leaks Nick Clifton
2022-12-23 15:35   ` Indu Bhagat
2022-12-23 15:39     ` Nick Clifton
2022-12-23 14:22 ` libsframe builder (Was: [PATCH 0/2] libsframe: fix some memory leaks) Mark Wielaard
2022-12-23 14:41   ` Frank Ch. Eigler
2022-12-23 16:06     ` Mark Wielaard
2022-12-23 15:24   ` Indu Bhagat
2022-12-23 16:13     ` Mark Wielaard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221222225457.1095930-2-indu.bhagat@oracle.com \
    --to=indu.bhagat@oracle.com \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).