vl: fix leak of qdict_crumple return value
PATCH 1/1 vl: fix leak of qdict_crumple return value
Coverity reports that qemu_parse_config_group is returning without unrefing the "crumpled" dictionary in case its top level item is a list. But actually the contract with qemu_record_config_group is the same as for qemu_parse_config_group itself: if those function need to stash the dictionary they get, they have to take a reference themselves (currently this is never the case for either function). Therefore, just add an unconditional qobject_unref(crumpled) to qemu_parse_config_group.
--- softmmu/vl.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
softmmu/vl.c +3 -2
@@ -2196,9 +2196,10 @@ static void qemu_parse_config_group(const char *group, QDict *qdict,if (qobject_type (crumpled) !=QTYPE_QDICT ) {assert (qobject_type (crumpled) ==QTYPE_QLIST );error_setg (errp,"Lists cannot be at top level of a configuration section" ); -return ; + }else { +qemu_record_config_group (group,qobject_to (QDict, crumpled), false, errp); } -qemu_record_config_group (group,qobject_to (QDict, crumpled), false, errp); +qobject_unref (crumpled); }static void qemu_read_default_config_file (Error **errp)
Signature · 1 line
2.31.1
Quoted 11 lines · Paolo Bonzini
Coverity reports that
qemu_parse_config_groupis returning without unrefing the "crumpled" dictionary in case its top level item is a list. But actually the contract withqemu_record_config_groupis the same as forqemu_parse_config_groupitself: if those function need to stash the dictionary they get, they have to take a reference themselves (currently this is never the case for either function). Therefore, just add an unconditionalqobject_unref(crumpled)toqemu_parse_config_group.Reported-by Peter Maydell <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>
Do we want
here?
Quoted 11 lines
--- softmmu/vl.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)softmmu/vl.c +0
-0@@ -2196,9 +2196,10 @@ static void qemu_parse_config_group(const char *group, QDict *qdict,if (qobject_type (crumpled) !=QTYPE_QDICT ) {assert (qobject_type (crumpled) ==QTYPE_QLIST );
softmmu/vl.c static void qemu_parse_config_group(const char *group, QDict *qdict, · line 2205
Quoted 10 lines
softmmu/vl.c +3
-2error_setg (errp,"Lists cannot be at top level of a configuration section" ); -return ; + }else { +qemu_record_config_group (group,qobject_to (QDict, crumpled), false, errp); } -qemu_record_config_group (group,qobject_to (QDict, crumpled), false, errp); +qobject_unref (crumpled); }static void qemu_read_default_config_file (Error **errp)
Minimally invasive fix, but the result is a bit awkward. Possibly neater:
if (qobject_type(crumpled) == QTYPE_QLIST) {
error_setg(errp,
"Lists cannot be at top level of a configuration section");
} else {
assert(qobject_type(crumpled) == QTYPE_QDICT);
qemu_record_config_group(group, qobject_to(QDict, crumpled),
false, errp);
}
qobject_unref(crumpled);Regardless:
Quoted 1 line · Markus Armbruster
Fixes c0d4aa82f895af67cbf7772324e05605e22b4162
Good point, will add.
Paolo
Quoted 12 lines · Markus Armbruster
Minimally invasive fix, but the result is a bit awkward. Possibly neater:
if (qobject_type(crumpled) == QTYPE_QLIST) { error_setg(errp, "Lists cannot be at top level of a configuration section"); } else { assert(qobject_type(crumpled) == QTYPE_QDICT); qemu_record_config_group(group, qobject_to(QDict, crumpled), false, errp); } qobject_unref(crumpled);
Even better:
switch (qobject_type(crumpled)) {
case QTYPE_QDICT:
qemu_record_config_group(group, qobject_to(QDict, crumpled), false, errp);
break;
case QTYPE_QLIST:
error_setg(errp, "Lists cannot be at top level of a configuration section");
break;
default:
g_assert_unreachable();
}Paolo