* [PATCH] hw/i386/fw_cfg: Use g_new() and g_new0() instead of g_malloc()
@ 2026-02-24 16:00 Sourish Dutta Sharma
2026-04-27 10:02 ` Thomas Huth
0 siblings, 1 reply; 3+ messages in thread
From: Sourish Dutta Sharma @ 2026-02-24 16:00 UTC (permalink / raw)
To: qemu-devel
Cc: pbonzini, richard.henderson, eduardo, mst, marcel.apfelbaum,
Sourish Dutta Sharma
Replace g_malloc() and g_malloc0() calls that calculate the allocation
size using sizeof() with the type-safe g_new() and g_new0() macros.
This aligns the code with QEMU's coding style guidelines, improving
readability and protecting against potential integer overflow
vulnerabilities when allocating arrays.
Signed-off-by: Sourish Duttta Sharma <sourishduttasharma770@gmail.com>
---
hw/i386/fw_cfg.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/hw/i386/fw_cfg.c b/hw/i386/fw_cfg.c
index 5670e8553e..ae1ba3b6a2 100644
--- a/hw/i386/fw_cfg.c
+++ b/hw/i386/fw_cfg.c
@@ -91,7 +91,7 @@ void fw_cfg_build_smbios(PCMachineState *pcms, FWCfgState *fw_cfg,
/* build the array of physical mem area from e820 table */
nr_e820 = e820_get_table(NULL);
- mem_array = g_malloc0(sizeof(*mem_array) * nr_e820);
+ mem_array = g_new0(struct smbios_phys_mem_area, nr_e820);
for (i = 0, array_count = 0; i < nr_e820; i++) {
uint64_t addr, len;
@@ -207,7 +207,7 @@ void fw_cfg_build_feature_control(MachineState *ms, FWCfgState *fw_cfg)
return;
}
- val = g_malloc(sizeof(*val));
+ val = g_new(uint64_t, 1);
*val = cpu_to_le64(feature_control_bits | FEATURE_CONTROL_LOCKED);
fw_cfg_add_file(fw_cfg, "etc/msr_feature_control", val, sizeof(*val));
}
--
2.51.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] hw/i386/fw_cfg: Use g_new() and g_new0() instead of g_malloc()
2026-02-24 16:00 [PATCH] hw/i386/fw_cfg: Use g_new() and g_new0() instead of g_malloc() Sourish Dutta Sharma
@ 2026-04-27 10:02 ` Thomas Huth
2026-04-27 10:04 ` Michael S. Tsirkin
0 siblings, 1 reply; 3+ messages in thread
From: Thomas Huth @ 2026-04-27 10:02 UTC (permalink / raw)
To: Sourish Dutta Sharma, qemu-devel
Cc: pbonzini, richard.henderson, eduardo, mst, marcel.apfelbaum
On 24/02/2026 17.00, Sourish Dutta Sharma wrote:
> Replace g_malloc() and g_malloc0() calls that calculate the allocation
> size using sizeof() with the type-safe g_new() and g_new0() macros.
>
> This aligns the code with QEMU's coding style guidelines, improving
> readability and protecting against potential integer overflow
> vulnerabilities when allocating arrays.
>
> Signed-off-by: Sourish Duttta Sharma <sourishduttasharma770@gmail.com>
> ---
> hw/i386/fw_cfg.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
Hi!
Thanks for your patch! FWIW, fw_cfg is not really my turf, but since I was
looking into malloc() related patches today, I noticed your patch which
seems to have fallen through the cracks (sorry for this!), so I picked it up
and added it to my latest pull request.
Regards,
Thomas
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] hw/i386/fw_cfg: Use g_new() and g_new0() instead of g_malloc()
2026-04-27 10:02 ` Thomas Huth
@ 2026-04-27 10:04 ` Michael S. Tsirkin
0 siblings, 0 replies; 3+ messages in thread
From: Michael S. Tsirkin @ 2026-04-27 10:04 UTC (permalink / raw)
To: Thomas Huth
Cc: Sourish Dutta Sharma, qemu-devel, pbonzini, richard.henderson,
eduardo, marcel.apfelbaum
On Mon, Apr 27, 2026 at 12:02:57PM +0200, Thomas Huth wrote:
> On 24/02/2026 17.00, Sourish Dutta Sharma wrote:
> > Replace g_malloc() and g_malloc0() calls that calculate the allocation
> > size using sizeof() with the type-safe g_new() and g_new0() macros.
> >
> > This aligns the code with QEMU's coding style guidelines, improving
> > readability and protecting against potential integer overflow
> > vulnerabilities when allocating arrays.
> >
> > Signed-off-by: Sourish Duttta Sharma <sourishduttasharma770@gmail.com>
> > ---
> > hw/i386/fw_cfg.c | 4 ++--
> > 1 file changed, 2 insertions(+), 2 deletions(-)
>
> Hi!
>
> Thanks for your patch! FWIW, fw_cfg is not really my turf, but since I was
> looking into malloc() related patches today, I noticed your patch which
> seems to have fallen through the cracks (sorry for this!), so I picked it up
> and added it to my latest pull request.
>
> Regards,
> Thomas
Thanks for doing this. FWIW
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-04-27 10:05 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-24 16:00 [PATCH] hw/i386/fw_cfg: Use g_new() and g_new0() instead of g_malloc() Sourish Dutta Sharma
2026-04-27 10:02 ` Thomas Huth
2026-04-27 10:04 ` Michael S. Tsirkin
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.