Skip to content

Commit

Permalink
nbd: Utilize QAPI_CLONE for type conversion
Browse files Browse the repository at this point in the history
Rather than open-coding the translation from the deprecated
NbdServerAddOptions type to the preferred BlockExportOptionsNbd, it's
better to utilize QAPI_CLONE_MEMBERS.  This solves a couple of issues:
first, if we do any more refactoring of the base type (which an
upcoming patch plans to do), we don't have to revisit the open-coding.
Second, our assignment to arg->name is fishy: the generated QAPI code
for qapi_free_NbdServerAddOptions does not visit arg->name if
arg->has_name is false, but if it DID visit it, we would have
introduced a double-free situation when arg is finally freed.

Signed-off-by: Eric Blake <[email protected]>
Reviewed-by: Vladimir Sementsov-Ogievskiy <[email protected]>
Reviewed-by: Markus Armbruster <[email protected]>
Message-Id: <[email protected]>
  • Loading branch information
ebblake committed Oct 30, 2020
1 parent 9812e71 commit 8675cbd
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions blockdev-nbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "sysemu/block-backend.h"
#include "hw/block/block.h"
#include "qapi/error.h"
#include "qapi/clone-visitor.h"
#include "qapi/qapi-visit-block-export.h"
#include "qapi/qapi-commands-block-export.h"
#include "block/nbd.h"
#include "io/channel-socket.h"
Expand Down Expand Up @@ -195,7 +197,8 @@ void qmp_nbd_server_add(NbdServerAddOptions *arg, Error **errp)
* the device name as a default here for compatibility.
*/
if (!arg->has_name) {
arg->name = arg->device;
arg->has_name = true;
arg->name = g_strdup(arg->device);
}

export_opts = g_new(BlockExportOptions, 1);
Expand All @@ -205,15 +208,9 @@ void qmp_nbd_server_add(NbdServerAddOptions *arg, Error **errp)
.node_name = g_strdup(bdrv_get_node_name(bs)),
.has_writable = arg->has_writable,
.writable = arg->writable,
.u.nbd = {
.has_name = true,
.name = g_strdup(arg->name),
.has_description = arg->has_description,
.description = g_strdup(arg->description),
.has_bitmap = arg->has_bitmap,
.bitmap = g_strdup(arg->bitmap),
},
};
QAPI_CLONE_MEMBERS(BlockExportOptionsNbd, &export_opts->u.nbd,
qapi_NbdServerAddOptions_base(arg));

/*
* nbd-server-add doesn't complain when a read-only device should be
Expand Down

0 comments on commit 8675cbd

Please sign in to comment.