Skip to contentSkip to conversation

qemu-pr-helper: do not free reply when multipath_process_cmd() fails

PATCH Applied Stefan Hajnoczi 3 participants 3 messages Oct 27 – Nov 2

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 qemu-pr-helper: do not free reply when multipath_process_cmd() fails

Kevin Wolf pointed out that the multipath_process_cmd() documentation only defines the value of reply on success. Don't touch it when the function returns -1.

The libmpathcmd implementation as of c78cf64a9e8e does not set *reply to NULL when its internal mpath_send_cmd() call fails, so I think Kevin's interpretation is correct.

As it stands, there is no observable bug in the QEMU code though because reply is initialized to NULL before calling multipath_process_cmd(). This patch is just a cleanup.

---
v2:
- Use g_autofree instead of manual free(3) calls [Eric]

---
 scsi/qemu-pr-helper.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
scsi/qemu-pr-helper.c +8 -3
index 54d196a503f..1f97a5c4bfe 100644
@@ -559,7 +559,7 @@ static char *get_dm_name(int fd, Error **errp)
 static bool multipathd_cmd(int fd, const char *cmd, Error **errp)
 {
     const unsigned int timeout_ms = 60000;
-    char *reply = NULL;
+    g_autofree char *reply = NULL;
     int last_errno;
     int ret;
 
@@ -568,11 +568,16 @@ static bool multipathd_cmd(int fd, const char *cmd, Error **errp)
     if (reply && strcmp(reply, "ok\n") != 0) {
         error_setg(errp, "mpath_process_cmd(%d, \"%s\") replied \"%s\"",
                    fd, cmd, reply);
-        free(reply);
         return false;
     }
-    free(reply);
     if (ret == -1) {
+        /*
+         * It should already be NULL but the mpath_process_cmd() documentation
+         * only defines it when the function returns success, so don't depend
+         * on its value when ret == -1.
+         */
+        reply = NULL;
+
         error_setg_errno(errp, last_errno,
                          "mpath_process_cmd(%d, \"%s\") failed",
                          fd, cmd);
Signature · 1 line
2.56.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 12 lines · Stefan Hajnoczi

Kevin Wolf pointed out that the multipath_process_cmd() documentation only defines the value of reply on success. Don't touch it when the function returns -1.

The libmpathcmd implementation as of c78cf64a9e8e does not set *reply to NULL when its internal mpath_send_cmd() call fails, so I think Kevin's interpretation is correct.

As it stands, there is no observable bug in the QEMU code though because reply is initialized to NULL before calling multipath_process_cmd(). This patch is just a cleanup.

Quoted 8 lines

---
v2:
- Use g_autofree instead of manual free(3) calls [Eric]

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org
Applied
#2 lore raw
In reply to Stefan Hajnoczi

Am 27.10.2026 um 21:07 hat Stefan Hajnoczi geschrieben:

Quoted 12 lines

Kevin Wolf pointed out that the multipath_process_cmd() documentation only defines the value of reply on success. Don't touch it when the function returns -1.

The libmpathcmd implementation as of c78cf64a9e8e does not set *reply to NULL when its internal mpath_send_cmd() call fails, so I think Kevin's interpretation is correct.

As it stands, there is no observable bug in the QEMU code though because reply is initialized to NULL before calling multipath_process_cmd(). This patch is just a cleanup.

Quoted 5 lines

Thanks, applied to the block branch.

Kevin