From bb4bd9d31fd21b6080d7416dda0f143a57ff866c Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Fri, 25 Oct 2024 09:41:21 +0200 Subject: [PATCH] Replace improper use of g_malloc(0) with g_new0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Completely remove use of g_malloc (without zeroing of the allocated memory) and forbid further use. Replace use of g_malloc0 in cases where the variable holding the pointer has proper type. In all of the above cases we can use g_new0 instead. Signed-off-by: Peter Krempa Reviewed-by: Ján Tomko --- build-aux/syntax-check.mk | 4 ++-- src/util/virpcivpd.c | 6 +++--- tools/wireshark/src/packet-libvirt.c | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/build-aux/syntax-check.mk b/build-aux/syntax-check.mk index 0759372b2b..6ed2a61192 100644 --- a/build-aux/syntax-check.mk +++ b/build-aux/syntax-check.mk @@ -92,8 +92,8 @@ sc_prohibit_raw_virclassnew: # Avoid raw malloc and free, except in documentation comments. sc_prohibit_raw_allocation: - @prohibit='^.[^*].*\<((m|c|re)alloc|free) *\([^)]' \ - halt='use VIR_ macros from viralloc.h instead of malloc/free' \ + @prohibit='^.[^*].*\<((m|c|re)alloc|free|g_malloc) *\([^)]' \ + halt='use g_new0/g_malloc0/g_free instead of malloc/free/g_malloc' \ $(_sc_search_regexp) # Avoid functions that can lead to double-close bugs. diff --git a/src/util/virpcivpd.c b/src/util/virpcivpd.c index 02d405f038..688d1a3fcb 100644 --- a/src/util/virpcivpd.c +++ b/src/util/virpcivpd.c @@ -414,7 +414,7 @@ virPCIVPDParseVPDLargeResourceFields(int vpdFileFd, virPCIVPDResource *res) { /* A buffer of up to one resource record field size (plus a zero byte) is needed. */ - g_autofree uint8_t *buf = g_malloc0(PCI_VPD_MAX_FIELD_SIZE + 1); + g_autofree uint8_t *buf = g_new0(uint8_t, PCI_VPD_MAX_FIELD_SIZE + 1); uint16_t fieldDataLen = 0, bytesToRead = 0; uint16_t fieldPos = resPos; bool hasChecksum = false; @@ -499,7 +499,7 @@ virPCIVPDParseVPDLargeResourceFields(int vpdFileFd, break; case VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_BINARY: - fieldValue = g_malloc(fieldDataLen); + fieldValue = g_new0(char, fieldDataLen); memcpy(fieldValue, buf, fieldDataLen); break; @@ -570,7 +570,7 @@ virPCIVPDParseVPDLargeResourceString(int vpdFileFd, uint16_t resPos, g_autofree char *resValue = NULL; /* The resource value is not NULL-terminated so add one more byte. */ - g_autofree char *buf = g_malloc0(resDataLen + 1); + g_autofree char *buf = g_new0(char, resDataLen + 1); if (virPCIVPDReadVPDBytes(vpdFileFd, (uint8_t *)buf, resDataLen, resPos, csum) < 0) return -1; diff --git a/tools/wireshark/src/packet-libvirt.c b/tools/wireshark/src/packet-libvirt.c index ee20e3734d..da2aabd98a 100644 --- a/tools/wireshark/src/packet-libvirt.c +++ b/tools/wireshark/src/packet-libvirt.c @@ -166,7 +166,7 @@ dissect_xdr_opaque(tvbuff_t *tvb, proto_tree *tree, XDR *xdrs, int hf, gboolean rc; guint8 *val; - val = g_malloc(size); + val = g_new0(guint8, size); start = xdr_getpos(xdrs); if ((rc = xdr_opaque(xdrs, (caddr_t)val, size))) { gint len = xdr_getpos(xdrs) - start;