From 63f1b8d9fbdb5961f2fe3a953b012abb229703c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Hozza?= Date: Mon, 24 Oct 2022 10:28:10 +0200 Subject: [PATCH] internal/disk: fix potential nil pointer dereference MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- internal/disk/lvm.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/internal/disk/lvm.go b/internal/disk/lvm.go index 1e4b2db11..b264f9d14 100644 --- a/internal/disk/lvm.go +++ b/internal/disk/lvm.go @@ -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) - if !cloneOk { - panic("LVMLogicalVolume.Clone() returned an Entity that cannot be converted to *LVMLogicalVolume; this is a programming error") - } - lv = lvEnt + + // 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") + } + clone.LogicalVolumes[idx] = *lv }