[03/10] libsframe: fix memory leak in sframe_decode
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
From: Indu Bhagat <indu.bhagat@oracle.com>
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(-)
Comments
On 8/28/2026 12:41 AM, Indu Bhagat wrote:
> From: Indu Bhagat <indu.bhagat@oracle.com>
>
> 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(-)
Reviewed-by: Jens Remus <jremus@linux.ibm.com>
> diff --git 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. */
Regards,
Jens
@@ -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. */