internal/disk: fix potential nil pointer dereference

The `LVMVolumeGroup.Clone()` method could end up dereferencing a `nil`
pointer in the `lv` variable, if there would be a `nil` logical volume
in the LVM volume group. Such situation would be an error of its own.
There is no point in checking if the cloned logical volume is not `nil`
and casting it to another variable. The logic should check if the cloned
logical volume is `nil` and panic in such situation. The following code
can then cast the clone to a different variable without issues and there
is no risk of dereferencing a `nil` pointer.

Signed-off-by: Tomáš Hozza <thozza@redhat.com>
This commit is contained in:
Tomáš Hozza 2022-10-24 10:28:10 +02:00 committed by Tomáš Hozza
parent 2735ea5b96
commit 63f1b8d9fb

View file

@ -29,14 +29,17 @@ func (vg *LVMVolumeGroup) Clone() Entity {
for idx, lv := range vg.LogicalVolumes {
ent := lv.Clone()
var lv *LVMLogicalVolume
if ent != nil {
lvEnt, cloneOk := ent.(*LVMLogicalVolume)
// lv.Clone() will return nil only if the logical volume is nil
if ent == nil {
panic(fmt.Sprintf("logical volume %d in a LVM volume group is nil; this is a programming error", idx))
}
lv, cloneOk := ent.(*LVMLogicalVolume)
if !cloneOk {
panic("LVMLogicalVolume.Clone() returned an Entity that cannot be converted to *LVMLogicalVolume; this is a programming error")
}
lv = lvEnt
}
clone.LogicalVolumes[idx] = *lv
}