]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
bhyve_command: Avoid leaking @buf in virBhyveProcessBuildBhyveCmd()
authorMichal Privoznik <mprivozn@redhat.com>
Mon, 2 Feb 2026 15:26:06 +0000 (16:26 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Wed, 4 Feb 2026 07:29:38 +0000 (08:29 +0100)
When building OS loader part of bhyve command line, there's @buf
declared and it is even correctly annotated with g_auto() to be
freed automatically. But then, the buffer contents is appended
onto the command line using virBufferContentAndReset() which
leads to a memleak because the buffer is reset. It's
virBufferCurrentContent() that should have been used instead.

128 bytes in 1 blocks are definitely lost in loss record 476 of 536
   at 0x48882B1: realloc (vg_replace_malloc.c:1810)
   by 0x4EE6622: g_realloc (in /usr/local/lib/libglib-2.0.so.0.8400.4)
   by 0x4F048BC: g_string_new (in /usr/local/lib/libglib-2.0.so.0.8400.4)
   by 0x4A59E1E: virBufferInitialize (virbuffer.c:121)
   by 0x4A5A63C: virBufferVasprintf (virbuffer.c:321)
   by 0x4A5A5DE: virBufferAsprintf (virbuffer.c:303)
   by 0x401B22F: virBhyveProcessBuildBhyveCmd (bhyve_command.c:1021)
   by 0x4015F05: testCompareXMLToArgvFiles (bhyvexml2argvtest.c:72)
   by 0x4015BA9: testCompareXMLToArgvHelper (bhyvexml2argvtest.c:144)
   by 0x4016588: virTestRun (testutils.c:143)
   by 0x4015919: mymain (bhyvexml2argvtest.c:341)
   by 0x4018882: virTestMain (testutils.c:913)

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Roman Bogorodskiy <bogorodskiy@gmail.com>
src/bhyve/bhyve_command.c

index a45a5d1c3322463328d79e777e5d977b813b4d1a..c9bfe22c8ca6037368fc8055b00dc079ad8d46e0 100644 (file)
@@ -1003,7 +1003,7 @@ virBhyveProcessBuildBhyveCmd(struct _bhyveConn *driver, virDomainDef *def,
     if (def->os.bootloader == NULL &&
         def->os.loader) {
         virArch arch = def->os.arch;
-            g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
+        g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
 
         if (ARCH_IS_X86(arch)) {
             if ((bhyveDriverGetBhyveCaps(driver) & BHYVE_CAP_LPC_BOOTROM)) {
@@ -1011,7 +1011,7 @@ virBhyveProcessBuildBhyveCmd(struct _bhyveConn *driver, virDomainDef *def,
                 if (def->os.loader->nvram && def->os.loader->nvram->path)
                     virBufferAsprintf(&buf, ",%s", def->os.loader->nvram->path);
 
-                virCommandAddArgList(cmd, "-l", virBufferContentAndReset(&buf), NULL);
+                virCommandAddArgList(cmd, "-l", virBufferCurrentContent(&buf), NULL);
             } else {
                 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                                _("Installed bhyve binary does not support UEFI loader"));
@@ -1019,7 +1019,7 @@ virBhyveProcessBuildBhyveCmd(struct _bhyveConn *driver, virDomainDef *def,
             }
         } else if (ARCH_IS_ARM(arch)) {
             virBufferAsprintf(&buf, "bootrom=%s", def->os.loader->path);
-            virCommandAddArgList(cmd, "-o", virBufferContentAndReset(&buf), NULL);
+            virCommandAddArgList(cmd, "-o", virBufferCurrentContent(&buf), NULL);
         }
     }