[PATCH 0/2] Forbid 'g_new' (the one without clearing memory)
I got burned by it in WIP patches. Let's not allow it anywhere. Peter Krempa (2): Replace all use of 'g_new' with 'g_new0' syntax-check: Prohibit the non-clearing 'g_new' build-aux/syntax-check.mk | 2 +- src/network/network_pf.c | 2 +- src/qemu/qemu_nbdkit.c | 2 +- src/qemu/qemu_process.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) -- 2.51.0
From: Peter Krempa <pkrempa@redhat.com> Always use the version which clears the allocated memory. Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- src/network/network_pf.c | 2 +- src/qemu/qemu_nbdkit.c | 2 +- src/qemu/qemu_process.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/network/network_pf.c b/src/network/network_pf.c index ce4461c999..f655a7a68b 100644 --- a/src/network/network_pf.c +++ b/src/network/network_pf.c @@ -131,7 +131,7 @@ findDefaultRouteInterface(void) if (i == RTAX_IFP && sa->sa_family == AF_LINK) { sdl = (struct sockaddr_dl *)(void *)sa; ifname_len = (sdl->sdl_nlen >= IFNAMSIZ) ? IFNAMSIZ - 1 : sdl->sdl_nlen; - ifname = g_new(char, ifname_len + 1); + ifname = g_new0(char, ifname_len + 1); virStrcpy(ifname, sdl->sdl_data, ifname_len + 1); return ifname; } diff --git a/src/qemu/qemu_nbdkit.c b/src/qemu/qemu_nbdkit.c index 65c98de017..c1bc6bc363 100644 --- a/src/qemu/qemu_nbdkit.c +++ b/src/qemu/qemu_nbdkit.c @@ -644,7 +644,7 @@ static qemuNbdkitProcessEventData* qemuNbdkitProcessEventDataNew(qemuNbdkitProcess *proc, virDomainObj *vm) { - qemuNbdkitProcessEventData *d = g_new(qemuNbdkitProcessEventData, 1); + qemuNbdkitProcessEventData *d = g_new0(qemuNbdkitProcessEventData, 1); d->proc = proc; d->vm = virObjectRef(vm); return d; diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index ead5bf3e48..57b14f640e 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -8704,7 +8704,7 @@ typedef struct { static qemuProcessInShutdownEventData* qemuProcessInShutdownEventDataNew(virDomainObj *vm, int pidfd) { - qemuProcessInShutdownEventData *d = g_new(qemuProcessInShutdownEventData, 1); + qemuProcessInShutdownEventData *d = g_new0(qemuProcessInShutdownEventData, 1); d->vm = virObjectRef(vm); d->pidfd = pidfd; return d; -- 2.51.0
From: Peter Krempa <pkrempa@redhat.com> Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- build-aux/syntax-check.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build-aux/syntax-check.mk b/build-aux/syntax-check.mk index 4ba059b56b..4deaa70368 100644 --- a/build-aux/syntax-check.mk +++ b/build-aux/syntax-check.mk @@ -92,7 +92,7 @@ sc_prohibit_raw_virclassnew: # Avoid raw malloc and free, except in documentation comments. sc_prohibit_raw_allocation: - @prohibit='^.[^*].*\<((m|c|re)alloc|free|g_malloc) *\([^)]' \ + @prohibit='^.[^*].*\<((m|c|re)alloc|free|g_malloc|g_new) *\([^)]' \ halt='use g_new0/g_malloc0/g_free instead of malloc/free/g_malloc' \ $(_sc_search_regexp) -- 2.51.0
On Tue, Oct 07, 2025 at 06:30:55PM +0200, Peter Krempa via Devel wrote:
I got burned by it in WIP patches. Let's not allow it anywhere.
Peter Krempa (2): Replace all use of 'g_new' with 'g_new0' syntax-check: Prohibit the non-clearing 'g_new'
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
participants (2)
-
Pavel Hrdina -
Peter Krempa