[PATCH] hw/audio/fmopl: Convert malloc, free to g_malloc0/g_new and g_free

jiputer posted 1 patch 2 weeks ago
Failed in applying to current master (apply log)
hw/audio/fmopl.c | 41 +++++++++++------------------------------
1 file changed, 11 insertions(+), 30 deletions(-)
[PATCH] hw/audio/fmopl: Convert malloc, free to g_malloc0/g_new and g_free
Posted by jiputer 2 weeks ago
In the function OPLOpenTable, replaced the usage of malloc with g_new
and removed the NULL checks meant for malloc.
In OPLCloseTable and OPLDestroy, replaced the usage of free to g_free.
In OPLCreate, replaced malloc with g_malloc0 and removed the NULL check
for ptr, and the now unnecessary memset.
The program will now abort when memory allocation fails instead of
checking if the pointer returned is null.

Signed-off-by: jiputer <jpaulma23@gmail.com>
---

A couple things to note, in the function OPL_LockTable, it checks the
return of OPLOpenTable but in the case of which memory allocation fails
it will abort before reaching this branch.

This patch belongs to this issue:
https://gitlab.com/qemu-project/qemu/-/work_items/1798

 hw/audio/fmopl.c | 41 +++++++++++------------------------------
 1 file changed, 11 insertions(+), 30 deletions(-)

diff --git a/hw/audio/fmopl.c b/hw/audio/fmopl.c
index a63ad0f04d..6bf4e74e80 100644
--- a/hw/audio/fmopl.c
+++ b/hw/audio/fmopl.c
@@ -607,26 +607,10 @@ static int OPLOpenTable( void )
 	double pom;
 
 	/* allocate dynamic tables */
-	if( (TL_TABLE = malloc(TL_MAX*2*sizeof(int32_t))) == NULL)
-		return 0;
-	if( (SIN_TABLE = malloc(SIN_ENT*4 *sizeof(int32_t *))) == NULL)
-	{
-		free(TL_TABLE);
-		return 0;
-	}
-	if( (AMS_TABLE = malloc(AMS_ENT*2 *sizeof(int32_t))) == NULL)
-	{
-		free(TL_TABLE);
-		free(SIN_TABLE);
-		return 0;
-	}
-	if( (VIB_TABLE = malloc(VIB_ENT*2 *sizeof(int32_t))) == NULL)
-	{
-		free(TL_TABLE);
-		free(SIN_TABLE);
-		free(AMS_TABLE);
-		return 0;
-	}
+    TL_TABLE = g_new(int32_t, TL_MAX * 2);
+    SIN_TABLE = g_new(int32_t *, SIN_ENT * 4);
+    AMS_TABLE = g_new(int32_t, AMS_ENT * 2);
+    VIB_TABLE = g_new(int32_t, VIB_ENT * 2);
     ENV_CURVE = g_new(int32_t, 2 * EG_ENT + 1);
 	/* make total level table */
 	for (t = 0;t < EG_ENT-1 ;t++){
@@ -696,10 +680,10 @@ static int OPLOpenTable( void )
 static void OPLCloseTable( void )
 {
     g_free(ENV_CURVE);
-	free(TL_TABLE);
-	free(SIN_TABLE);
-	free(AMS_TABLE);
-	free(VIB_TABLE);
+    g_free(TL_TABLE);
+    g_free(SIN_TABLE);
+    g_free(AMS_TABLE);
+    g_free(VIB_TABLE);
 }
 
 /* CSM Key Control */
@@ -1081,11 +1065,8 @@ FM_OPL *OPLCreate(int clock, int rate)
 	/* allocate OPL state space */
 	state_size  = sizeof(FM_OPL);
 	state_size += sizeof(OPL_CH)*max_ch;
-	/* allocate memory block */
-	ptr = malloc(state_size);
-	if(ptr==NULL) return NULL;
-	/* clear */
-	memset(ptr,0,state_size);
+    /* allocate memory block and set to 0 */
+    ptr = g_malloc0(state_size);
 	OPL        = (FM_OPL *)ptr; ptr+=sizeof(FM_OPL);
 	OPL->P_CH  = (OPL_CH *)ptr; ptr+=sizeof(OPL_CH)*max_ch;
 	/* set channel state pointer */
@@ -1128,7 +1109,7 @@ void OPLDestroy(FM_OPL *OPL)
 	}
 #endif
 	OPL_UnLockTable();
-	free(OPL);
+    g_free(OPL);
 }
 
 /* ----------  Option handlers ----------       */
-- 
2.43.0
Re: [PATCH] hw/audio/fmopl: Convert malloc, free to g_malloc0/g_new and g_free
Posted by Marc-André Lureau 1 week, 6 days ago
On Mon, Nov 9, 2026 at 12:40 AM jiputer <jpaulma23@gmail.com> wrote:
>
> In the function OPLOpenTable, replaced the usage of malloc with g_new
> and removed the NULL checks meant for malloc.
> In OPLCloseTable and OPLDestroy, replaced the usage of free to g_free.
> In OPLCreate, replaced malloc with g_malloc0 and removed the NULL check
> for ptr, and the now unnecessary memset.
> The program will now abort when memory allocation fails instead of
> checking if the pointer returned is null.
>
> Signed-off-by: jiputer <jpaulma23@gmail.com>

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>

> ---
>
> A couple things to note, in the function OPL_LockTable, it checks the
> return of OPLOpenTable but in the case of which memory allocation fails
> it will abort before reaching this branch.
>
> This patch belongs to this issue:
> https://gitlab.com/qemu-project/qemu/-/work_items/1798
>
>  hw/audio/fmopl.c | 41 +++++++++++------------------------------
>  1 file changed, 11 insertions(+), 30 deletions(-)
>
> diff --git a/hw/audio/fmopl.c b/hw/audio/fmopl.c
> index a63ad0f04d..6bf4e74e80 100644
> --- a/hw/audio/fmopl.c
> +++ b/hw/audio/fmopl.c
> @@ -607,26 +607,10 @@ static int OPLOpenTable( void )
>         double pom;
>
>         /* allocate dynamic tables */
> -       if( (TL_TABLE = malloc(TL_MAX*2*sizeof(int32_t))) == NULL)
> -               return 0;
> -       if( (SIN_TABLE = malloc(SIN_ENT*4 *sizeof(int32_t *))) == NULL)
> -       {
> -               free(TL_TABLE);
> -               return 0;
> -       }
> -       if( (AMS_TABLE = malloc(AMS_ENT*2 *sizeof(int32_t))) == NULL)
> -       {
> -               free(TL_TABLE);
> -               free(SIN_TABLE);
> -               return 0;
> -       }
> -       if( (VIB_TABLE = malloc(VIB_ENT*2 *sizeof(int32_t))) == NULL)
> -       {
> -               free(TL_TABLE);
> -               free(SIN_TABLE);
> -               free(AMS_TABLE);
> -               return 0;
> -       }
> +    TL_TABLE = g_new(int32_t, TL_MAX * 2);
> +    SIN_TABLE = g_new(int32_t *, SIN_ENT * 4);
> +    AMS_TABLE = g_new(int32_t, AMS_ENT * 2);
> +    VIB_TABLE = g_new(int32_t, VIB_ENT * 2);
>      ENV_CURVE = g_new(int32_t, 2 * EG_ENT + 1);
>         /* make total level table */
>         for (t = 0;t < EG_ENT-1 ;t++){
> @@ -696,10 +680,10 @@ static int OPLOpenTable( void )
>  static void OPLCloseTable( void )
>  {
>      g_free(ENV_CURVE);
> -       free(TL_TABLE);
> -       free(SIN_TABLE);
> -       free(AMS_TABLE);
> -       free(VIB_TABLE);
> +    g_free(TL_TABLE);
> +    g_free(SIN_TABLE);
> +    g_free(AMS_TABLE);
> +    g_free(VIB_TABLE);
>  }
>
>  /* CSM Key Control */
> @@ -1081,11 +1065,8 @@ FM_OPL *OPLCreate(int clock, int rate)
>         /* allocate OPL state space */
>         state_size  = sizeof(FM_OPL);
>         state_size += sizeof(OPL_CH)*max_ch;
> -       /* allocate memory block */
> -       ptr = malloc(state_size);
> -       if(ptr==NULL) return NULL;
> -       /* clear */
> -       memset(ptr,0,state_size);
> +    /* allocate memory block and set to 0 */
> +    ptr = g_malloc0(state_size);
>         OPL        = (FM_OPL *)ptr; ptr+=sizeof(FM_OPL);
>         OPL->P_CH  = (OPL_CH *)ptr; ptr+=sizeof(OPL_CH)*max_ch;
>         /* set channel state pointer */
> @@ -1128,7 +1109,7 @@ void OPLDestroy(FM_OPL *OPL)
>         }
>  #endif
>         OPL_UnLockTable();
> -       free(OPL);
> +    g_free(OPL);
>  }
>
>  /* ----------  Option handlers ----------       */
> --
> 2.43.0
>
>


-- 
Marc-André Lureau
Re: [PATCH] hw/audio/fmopl: Convert malloc, free to g_malloc0/g_new and g_free
Posted by Peter Maydell 1 week, 6 days ago
On Sun, 8 Nov 2026 at 20:39, jiputer <jpaulma23@gmail.com> wrote:
>
> In the function OPLOpenTable, replaced the usage of malloc with g_new
> and removed the NULL checks meant for malloc.
> In OPLCloseTable and OPLDestroy, replaced the usage of free to g_free.
> In OPLCreate, replaced malloc with g_malloc0 and removed the NULL check
> for ptr, and the now unnecessary memset.
> The program will now abort when memory allocation fails instead of
> checking if the pointer returned is null.
>
> Signed-off-by: jiputer <jpaulma23@gmail.com>

There is already a fix for this one on the list, picked up by
the -trivial patchset; it just hasn't reached git yet.

https://lore.kernel.org/qemu-devel/623429ea-d915-480c-bd76-3052d85bea7d@tls.msk.ru/

(This is why #1798 says "see the on-list discussion first" about that one.)

thanks
-- PMM