ch_monitor: Avoid possible double free in virCHMonitorClose()

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 <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Michal Privoznik 2024-11-11 14:40:32 +01:00
parent 4be361a385
commit f1f4cbb50a

View File

@ -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);