From f70a6ea97da90eeda86fc2ca115ea66b68feaaec Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Mon, 23 Sep 2019 12:53:04 +0200 Subject: [PATCH] api: config deletion: avoid regex on undefined values Signed-off-by: Thomas Lamprecht --- PVE/API2/Qemu.pm | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm index 5814f941..fd09d501 100644 --- a/PVE/API2/Qemu.pm +++ b/PVE/API2/Qemu.pm @@ -1180,14 +1180,18 @@ my $update_vm_api = sub { foreach my $opt (@delete) { $modified->{$opt} = 1; $conf = PVE::QemuConfig->load_config($vmid); # update/reload - if (!defined($conf->{$opt}) && !defined($conf->{pending}->{$opt})) { + + # value of what we want to delete, independent if pending or not + my $val = $conf->{$opt} // $conf->{pending}->{$opt}; + if (!defined($val)) { warn "cannot delete '$opt' - not set in current configuration!\n"; $modified->{$opt} = 0; next; } + my $is_pending_val = defined($conf->{pending}->{$opt}); if ($opt =~ m/^unused/) { - my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt}); + my $drive = PVE::QemuServer::parse_drive($opt, $val); PVE::QemuConfig->check_protection($conf, "can't remove unused disk '$drive->{file}'"); $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']); if (PVE::QemuServer::try_deallocate_drive($storecfg, $vmid, $conf, $opt, $drive, $rpcenv, $authuser)) { @@ -1197,12 +1201,12 @@ my $update_vm_api = sub { } elsif (PVE::QemuServer::is_valid_drivename($opt)) { PVE::QemuConfig->check_protection($conf, "can't remove drive '$opt'"); $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']); - PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $conf->{pending}->{$opt})) - if defined($conf->{pending}->{$opt}); + PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $val)) + if $is_pending_val; PVE::QemuServer::vmconfig_delete_pending_option($conf, $opt, $force); PVE::QemuConfig->write_config($vmid, $conf); } elsif ($opt =~ m/^serial\d+$/) { - if ($conf->{$opt} eq 'socket' || (!$conf->{$opt} && $conf->{pending}->{$opt} eq 'socket')) { + if ($val eq 'socket') { $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']); } elsif ($authuser ne 'root@pam') { die "only root can delete '$opt' config for real devices\n"; @@ -1210,7 +1214,7 @@ my $update_vm_api = sub { PVE::QemuServer::vmconfig_delete_pending_option($conf, $opt, $force); PVE::QemuConfig->write_config($vmid, $conf); } elsif ($opt =~ m/^usb\d+$/) { - if ($conf->{$opt} =~ m/spice/ || (!$conf->{$opt} && $conf->{pending}->{$opt} =~ m/spice/)) { + if ($val =~ m/spice/) { $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']); } elsif ($authuser ne 'root@pam') { die "only root can delete '$opt' config for real devices\n"; -- 2.39.5