[PATCH 0/2] Fix memory leaks
Pavel Hrdina (2): virNetworkIPDefFormat: Fix memory leaks chSocketRecv: Fix memory leak src/ch/ch_process.c | 2 +- src/conf/network_conf.c | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) -- 2.53.0
From: Pavel Hrdina <phrdina@redhat.com> Use g_auto() for every virBuffer in this function to make sure none of them will leak memory. It is not necessary to use on all of them because for some of the buffers virXMLFormatElement() is called before any return from the function but for consistency reasons it's better to use g_auto() for all cases. Fixes: d9b34ad12b2da231431a761b03ca038cdd44bd42 Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/conf/network_conf.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c index 17ef1c2a08..abd4c6eb4e 100644 --- a/src/conf/network_conf.c +++ b/src/conf/network_conf.c @@ -2065,8 +2065,8 @@ static int virNetworkIPDefFormat(virBuffer *buf, const virNetworkIPDef *def) { - virBuffer ipAttrBuf = VIR_BUFFER_INITIALIZER; - virBuffer ipChildBuf = VIR_BUFFER_INIT_CHILD(buf); + g_auto(virBuffer) ipAttrBuf = VIR_BUFFER_INITIALIZER; + g_auto(virBuffer) ipChildBuf = VIR_BUFFER_INIT_CHILD(buf); if (def->family) virBufferAsprintf(&ipAttrBuf, " family='%s'", def->family); @@ -2093,12 +2093,12 @@ virNetworkIPDefFormat(virBuffer *buf, virBufferEscapeString(&ipChildBuf, "<tftp root='%s'/>\n", def->tftproot); if ((def->nranges || def->nhosts)) { - virBuffer dhcpChildBuf = VIR_BUFFER_INIT_CHILD(&ipChildBuf); + g_auto(virBuffer) dhcpChildBuf = VIR_BUFFER_INIT_CHILD(&ipChildBuf); size_t i; for (i = 0; i < def->nranges; i++) { - virBuffer rangeAttrBuf = VIR_BUFFER_INITIALIZER; - virBuffer rangeChildBuf = VIR_BUFFER_INIT_CHILD(&dhcpChildBuf); + g_auto(virBuffer) rangeAttrBuf = VIR_BUFFER_INITIALIZER; + g_auto(virBuffer) rangeChildBuf = VIR_BUFFER_INIT_CHILD(&dhcpChildBuf); virSocketAddrRange addr = def->ranges[i].addr; virNetworkDHCPLeaseTimeDef *lease = def->ranges[i].lease; g_autofree char *saddr = NULL; @@ -2125,8 +2125,8 @@ virNetworkIPDefFormat(virBuffer *buf, virXMLFormatElement(&dhcpChildBuf, "range", &rangeAttrBuf, &rangeChildBuf); } for (i = 0; i < def->nhosts; i++) { - virBuffer hostAttrBuf = VIR_BUFFER_INITIALIZER; - virBuffer hostChildBuf = VIR_BUFFER_INIT_CHILD(&dhcpChildBuf); + g_auto(virBuffer) hostAttrBuf = VIR_BUFFER_INITIALIZER; + g_auto(virBuffer) hostChildBuf = VIR_BUFFER_INIT_CHILD(&dhcpChildBuf); virNetworkDHCPLeaseTimeDef *lease = def->hosts[i].lease; if (def->hosts[i].mac) @@ -2155,7 +2155,7 @@ virNetworkIPDefFormat(virBuffer *buf, virXMLFormatElement(&dhcpChildBuf, "host", &hostAttrBuf, &hostChildBuf); } if (def->bootfile) { - virBuffer bootpAttrBuf = VIR_BUFFER_INITIALIZER; + g_auto(virBuffer) bootpAttrBuf = VIR_BUFFER_INITIALIZER; virBufferEscapeString(&bootpAttrBuf, " file='%s'", def->bootfile); if (VIR_SOCKET_ADDR_VALID(&def->bootserver)) { -- 2.53.0
On Fri, Apr 10, 2026 at 16:12:07 +0200, Pavel Hrdina via Devel wrote:
From: Pavel Hrdina <phrdina@redhat.com>
Use g_auto() for every virBuffer in this function to make sure none of them will leak memory. It is not necessary to use on all of them because for some of the buffers virXMLFormatElement() is called before any return from the function but for consistency reasons it's better to use g_auto() for all cases.
Fixes: d9b34ad12b2da231431a761b03ca038cdd44bd42 Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/conf/network_conf.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
From: Pavel Hrdina <phrdina@redhat.com> Add missing g_autofree as the code looks like it was already written to use it. Fixes: 6f55137a1c465fde5aabca4530031542f751b4ad Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/ch/ch_process.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ch/ch_process.c b/src/ch/ch_process.c index 29db853a7f..a67274b4a5 100644 --- a/src/ch/ch_process.c +++ b/src/ch/ch_process.c @@ -548,7 +548,7 @@ static char * chSocketRecv(int sock, bool use_timeout) { struct pollfd pfds[1]; - char *buf = NULL; + g_autofree char *buf = NULL; size_t buf_len = 1024; int timeout = PKT_TIMEOUT_MS; int ret; -- 2.53.0
On Fri, Apr 10, 2026 at 16:12:08 +0200, Pavel Hrdina via Devel wrote:
From: Pavel Hrdina <phrdina@redhat.com>
Add missing g_autofree as the code looks like it was already written to use it.
Fixes: 6f55137a1c465fde5aabca4530031542f751b4ad
This reference doesn't make sense: commit 6f55137a1c465fde5aabca4530031542f751b4ad Author: Michal Prívozník <mprivozn@redhat.com> Date: Fri Feb 2 12:49:22 2024 +0100 virsocket: Drop unused #include and #define Inside of virsocket.c there is an include of poll.h and PKT_TIMEOUT_MS macro definition. Neither of these is really needed and in fact it's a leftover after I reworked one of previously merged commits during review. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Ján Tomko <jtomko@redhat.com> diff --git a/src/util/virsocket.c b/src/util/virsocket.c index 3b274a4eec..a4c646e759 100644 --- a/src/util/virsocket.c +++ b/src/util/virsocket.c @@ -26,9 +26,6 @@ #include "virlog.h" #include <fcntl.h> -#include <poll.h> - -#define PKT_TIMEOUT_MS 500 /* ms */ #define VIR_FROM_THIS VIR_FROM_NONE IMO it's 6316f26cd2d which added 'chSocketRecv'
Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/ch/ch_process.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/ch/ch_process.c b/src/ch/ch_process.c index 29db853a7f..a67274b4a5 100644 --- a/src/ch/ch_process.c +++ b/src/ch/ch_process.c @@ -548,7 +548,7 @@ static char * chSocketRecv(int sock, bool use_timeout) { struct pollfd pfds[1]; - char *buf = NULL; + g_autofree char *buf = NULL; size_t buf_len = 1024; int timeout = PKT_TIMEOUT_MS; int ret;
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
On Tue, Apr 14, 2026 at 10:19:56AM +0200, Peter Krempa via Devel wrote:
On Fri, Apr 10, 2026 at 16:12:08 +0200, Pavel Hrdina via Devel wrote:
From: Pavel Hrdina <phrdina@redhat.com>
Add missing g_autofree as the code looks like it was already written to use it.
Fixes: 6f55137a1c465fde5aabca4530031542f751b4ad
This reference doesn't make sense:
commit 6f55137a1c465fde5aabca4530031542f751b4ad Author: Michal Prívozník <mprivozn@redhat.com> Date: Fri Feb 2 12:49:22 2024 +0100
virsocket: Drop unused #include and #define
Inside of virsocket.c there is an include of poll.h and PKT_TIMEOUT_MS macro definition. Neither of these is really needed and in fact it's a leftover after I reworked one of previously merged commits during review.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Ján Tomko <jtomko@redhat.com>
diff --git a/src/util/virsocket.c b/src/util/virsocket.c index 3b274a4eec..a4c646e759 100644 --- a/src/util/virsocket.c +++ b/src/util/virsocket.c @@ -26,9 +26,6 @@ #include "virlog.h"
#include <fcntl.h> -#include <poll.h> - -#define PKT_TIMEOUT_MS 500 /* ms */
#define VIR_FROM_THIS VIR_FROM_NONE
IMO it's 6316f26cd2d which added 'chSocketRecv'
Yeah looking at git history these two commits are next to each other so I probably missclicked. Will fix before pushing. Pavel
Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/ch/ch_process.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/ch/ch_process.c b/src/ch/ch_process.c index 29db853a7f..a67274b4a5 100644 --- a/src/ch/ch_process.c +++ b/src/ch/ch_process.c @@ -548,7 +548,7 @@ static char * chSocketRecv(int sock, bool use_timeout) { struct pollfd pfds[1]; - char *buf = NULL; + g_autofree char *buf = NULL; size_t buf_len = 1024; int timeout = PKT_TIMEOUT_MS; int ret;
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
participants (3)
-
Pavel Hrdina -
Pavel Hrdina -
Peter Krempa