smbios: Clean up error handling in smbios_add()

Calling error_report() in a function that takes an Error ** argument
is suspicious.  smbios_entry_add() does that, and then exit()s.  It
also passes &error_fatal to qemu_opts_validate().  Both wrong, but
currently harmless, as its only caller passes &error_fatal.  Messed up
in commit 1007a37e20.  Clean it up.

Cc: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Acked-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20181017082702.5581-12-armbru@redhat.com>
This commit is contained in:
Markus Armbruster 2018-10-17 10:26:35 +02:00
parent 11ab69d6e7
commit 1028283c60

View File

@ -950,6 +950,7 @@ static void save_opt_list(size_t *ndest, const char ***dest,
void smbios_entry_add(QemuOpts *opts, Error **errp) void smbios_entry_add(QemuOpts *opts, Error **errp)
{ {
Error *err = NULL;
const char *val; const char *val;
assert(!smbios_immutable); assert(!smbios_immutable);
@ -960,12 +961,16 @@ void smbios_entry_add(QemuOpts *opts, Error **errp)
int size; int size;
struct smbios_table *table; /* legacy mode only */ struct smbios_table *table; /* legacy mode only */
qemu_opts_validate(opts, qemu_smbios_file_opts, &error_fatal); qemu_opts_validate(opts, qemu_smbios_file_opts, &err);
if (err) {
error_propagate(errp, err);
return;
}
size = get_image_size(val); size = get_image_size(val);
if (size == -1 || size < sizeof(struct smbios_structure_header)) { if (size == -1 || size < sizeof(struct smbios_structure_header)) {
error_report("Cannot read SMBIOS file %s", val); error_setg(errp, "Cannot read SMBIOS file %s", val);
exit(1); return;
} }
/* /*
@ -978,14 +983,15 @@ void smbios_entry_add(QemuOpts *opts, Error **errp)
smbios_tables_len); smbios_tables_len);
if (load_image(val, (uint8_t *)header) != size) { if (load_image(val, (uint8_t *)header) != size) {
error_report("Failed to load SMBIOS file %s", val); error_setg(errp, "Failed to load SMBIOS file %s", val);
exit(1); return;
} }
if (test_bit(header->type, have_fields_bitmap)) { if (test_bit(header->type, have_fields_bitmap)) {
error_report("can't load type %d struct, fields already specified!", error_setg(errp,
"can't load type %d struct, fields already specified!",
header->type); header->type);
exit(1); return;
} }
set_bit(header->type, have_binfile_bitmap); set_bit(header->type, have_binfile_bitmap);
@ -1030,19 +1036,23 @@ void smbios_entry_add(QemuOpts *opts, Error **errp)
unsigned long type = strtoul(val, NULL, 0); unsigned long type = strtoul(val, NULL, 0);
if (type > SMBIOS_MAX_TYPE) { if (type > SMBIOS_MAX_TYPE) {
error_report("out of range!"); error_setg(errp, "out of range!");
exit(1); return;
} }
if (test_bit(type, have_binfile_bitmap)) { if (test_bit(type, have_binfile_bitmap)) {
error_report("can't add fields, binary file already loaded!"); error_setg(errp, "can't add fields, binary file already loaded!");
exit(1); return;
} }
set_bit(type, have_fields_bitmap); set_bit(type, have_fields_bitmap);
switch (type) { switch (type) {
case 0: case 0:
qemu_opts_validate(opts, qemu_smbios_type0_opts, &error_fatal); qemu_opts_validate(opts, qemu_smbios_type0_opts, &err);
if (err) {
error_propagate(errp, err);
return;
}
save_opt(&type0.vendor, opts, "vendor"); save_opt(&type0.vendor, opts, "vendor");
save_opt(&type0.version, opts, "version"); save_opt(&type0.version, opts, "version");
save_opt(&type0.date, opts, "date"); save_opt(&type0.date, opts, "date");
@ -1051,14 +1061,18 @@ void smbios_entry_add(QemuOpts *opts, Error **errp)
val = qemu_opt_get(opts, "release"); val = qemu_opt_get(opts, "release");
if (val) { if (val) {
if (sscanf(val, "%hhu.%hhu", &type0.major, &type0.minor) != 2) { if (sscanf(val, "%hhu.%hhu", &type0.major, &type0.minor) != 2) {
error_report("Invalid release"); error_setg(errp, "Invalid release");
exit(1); return;
} }
type0.have_major_minor = true; type0.have_major_minor = true;
} }
return; return;
case 1: case 1:
qemu_opts_validate(opts, qemu_smbios_type1_opts, &error_fatal); qemu_opts_validate(opts, qemu_smbios_type1_opts, &err);
if (err) {
error_propagate(errp, err);
return;
}
save_opt(&type1.manufacturer, opts, "manufacturer"); save_opt(&type1.manufacturer, opts, "manufacturer");
save_opt(&type1.product, opts, "product"); save_opt(&type1.product, opts, "product");
save_opt(&type1.version, opts, "version"); save_opt(&type1.version, opts, "version");
@ -1069,14 +1083,18 @@ void smbios_entry_add(QemuOpts *opts, Error **errp)
val = qemu_opt_get(opts, "uuid"); val = qemu_opt_get(opts, "uuid");
if (val) { if (val) {
if (qemu_uuid_parse(val, &qemu_uuid) != 0) { if (qemu_uuid_parse(val, &qemu_uuid) != 0) {
error_report("Invalid UUID"); error_setg(errp, "Invalid UUID");
exit(1); return;
} }
qemu_uuid_set = true; qemu_uuid_set = true;
} }
return; return;
case 2: case 2:
qemu_opts_validate(opts, qemu_smbios_type2_opts, &error_fatal); qemu_opts_validate(opts, qemu_smbios_type2_opts, &err);
if (err) {
error_propagate(errp, err);
return;
}
save_opt(&type2.manufacturer, opts, "manufacturer"); save_opt(&type2.manufacturer, opts, "manufacturer");
save_opt(&type2.product, opts, "product"); save_opt(&type2.product, opts, "product");
save_opt(&type2.version, opts, "version"); save_opt(&type2.version, opts, "version");
@ -1085,7 +1103,11 @@ void smbios_entry_add(QemuOpts *opts, Error **errp)
save_opt(&type2.location, opts, "location"); save_opt(&type2.location, opts, "location");
return; return;
case 3: case 3:
qemu_opts_validate(opts, qemu_smbios_type3_opts, &error_fatal); qemu_opts_validate(opts, qemu_smbios_type3_opts, &err);
if (err) {
error_propagate(errp, err);
return;
}
save_opt(&type3.manufacturer, opts, "manufacturer"); save_opt(&type3.manufacturer, opts, "manufacturer");
save_opt(&type3.version, opts, "version"); save_opt(&type3.version, opts, "version");
save_opt(&type3.serial, opts, "serial"); save_opt(&type3.serial, opts, "serial");
@ -1093,7 +1115,11 @@ void smbios_entry_add(QemuOpts *opts, Error **errp)
save_opt(&type3.sku, opts, "sku"); save_opt(&type3.sku, opts, "sku");
return; return;
case 4: case 4:
qemu_opts_validate(opts, qemu_smbios_type4_opts, &error_fatal); qemu_opts_validate(opts, qemu_smbios_type4_opts, &err);
if (err) {
error_propagate(errp, err);
return;
}
save_opt(&type4.sock_pfx, opts, "sock_pfx"); save_opt(&type4.sock_pfx, opts, "sock_pfx");
save_opt(&type4.manufacturer, opts, "manufacturer"); save_opt(&type4.manufacturer, opts, "manufacturer");
save_opt(&type4.version, opts, "version"); save_opt(&type4.version, opts, "version");
@ -1102,11 +1128,19 @@ void smbios_entry_add(QemuOpts *opts, Error **errp)
save_opt(&type4.part, opts, "part"); save_opt(&type4.part, opts, "part");
return; return;
case 11: case 11:
qemu_opts_validate(opts, qemu_smbios_type11_opts, &error_fatal); qemu_opts_validate(opts, qemu_smbios_type11_opts, &err);
if (err) {
error_propagate(errp, err);
return;
}
save_opt_list(&type11.nvalues, &type11.values, opts, "value"); save_opt_list(&type11.nvalues, &type11.values, opts, "value");
return; return;
case 17: case 17:
qemu_opts_validate(opts, qemu_smbios_type17_opts, &error_fatal); qemu_opts_validate(opts, qemu_smbios_type17_opts, &err);
if (err) {
error_propagate(errp, err);
return;
}
save_opt(&type17.loc_pfx, opts, "loc_pfx"); save_opt(&type17.loc_pfx, opts, "loc_pfx");
save_opt(&type17.bank, opts, "bank"); save_opt(&type17.bank, opts, "bank");
save_opt(&type17.manufacturer, opts, "manufacturer"); save_opt(&type17.manufacturer, opts, "manufacturer");
@ -1116,12 +1150,12 @@ void smbios_entry_add(QemuOpts *opts, Error **errp)
type17.speed = qemu_opt_get_number(opts, "speed", 0); type17.speed = qemu_opt_get_number(opts, "speed", 0);
return; return;
default: default:
error_report("Don't know how to build fields for SMBIOS type %ld", error_setg(errp,
"Don't know how to build fields for SMBIOS type %ld",
type); type);
exit(1); return;
} }
} }
error_report("Must specify type= or file="); error_setg(errp, "Must specify type= or file=");
exit(1);
} }