From f1f4cbb50a943da5f0b0451e0a32f57bdcd82319 Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Mon, 11 Nov 2024 14:40:32 +0100 Subject: [PATCH] ch_monitor: Avoid possible double free in virCHMonitorClose() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit The virCHMonitorClose() is meant to be called when monitor to cloud-hypervisor process closes. It removes the socket and frees string containing path to the socket. In general, there is a problem with the following pattern: if (var) { do_something(); g_free(var); } because if the pattern executes twice the variable is freed twice. That's why we have VIR_FREE() macro. Well, replace plain g_free() with g_clear_pointer(). Mind you, this is NOT a destructor where clearing pointers is needless. Signed-off-by: Michal Privoznik Reviewed-by: Ján Tomko --- src/ch/ch_monitor.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ch/ch_monitor.c b/src/ch/ch_monitor.c index ccd04cfbd1..18ca5a764e 100644 --- a/src/ch/ch_monitor.c +++ b/src/ch/ch_monitor.c @@ -626,7 +626,7 @@ void virCHMonitorClose(virCHMonitor *mon) VIR_WARN("Unable to remove CH socket file '%s'", mon->socketpath); } - g_free(mon->socketpath); + g_clear_pointer(&mon->socketpath, g_free); } virObjectUnref(mon); -- 2.47.2