bugfix: fix potential panic

* Fix potential panic by nil pointer.
* Address comments.

Signed-off-by: Wei Zhang <zhangwei555@huawei.com>
This commit is contained in:
Wei Zhang 2019-04-19 15:28:27 +08:00
parent 9bd4e5008c
commit 3262da0207
4 changed files with 30 additions and 26 deletions

View File

@ -153,6 +153,7 @@ func (device *BlockDevice) Dump() persistapi.DeviceState {
ds.Type = string(device.DeviceType()) ds.Type = string(device.DeviceType())
drive := device.BlockDrive drive := device.BlockDrive
if drive != nil {
ds.BlockDrive = &persistapi.BlockDrive{ ds.BlockDrive = &persistapi.BlockDrive{
File: drive.File, File: drive.File,
Format: drive.Format, Format: drive.Format,
@ -164,6 +165,7 @@ func (device *BlockDevice) Dump() persistapi.DeviceState {
NvdimmID: drive.NvdimmID, NvdimmID: drive.NvdimmID,
VirtPath: drive.VirtPath, VirtPath: drive.VirtPath,
} }
}
return ds return ds
} }

View File

@ -119,16 +119,19 @@ func (device *GenericDevice) bumpAttachCount(attach bool) (skip bool, err error)
// Dump convert and return data in persist format // Dump convert and return data in persist format
func (device *GenericDevice) Dump() persistapi.DeviceState { func (device *GenericDevice) Dump() persistapi.DeviceState {
info := device.DeviceInfo dss := persistapi.DeviceState{
return persistapi.DeviceState{
ID: device.ID, ID: device.ID,
Type: string(device.DeviceType()), Type: string(device.DeviceType()),
RefCount: device.RefCount, RefCount: device.RefCount,
AttachCount: device.AttachCount, AttachCount: device.AttachCount,
}
DevType: info.DevType, info := device.DeviceInfo
Major: info.Major, if info != nil {
Minor: info.Minor, dss.DevType = info.DevType
DriverOptions: info.DriverOptions, dss.Major = info.Major
dss.Minor = info.Minor
dss.DriverOptions = info.DriverOptions
} }
return dss
} }

View File

@ -147,6 +147,7 @@ func (device *VFIODevice) Dump() persistapi.DeviceState {
devs := device.VfioDevs devs := device.VfioDevs
for _, dev := range devs { for _, dev := range devs {
if dev != nil {
ds.VFIODevs = append(ds.VFIODevs, &persistapi.VFIODev{ ds.VFIODevs = append(ds.VFIODevs, &persistapi.VFIODev{
ID: dev.ID, ID: dev.ID,
Type: string(dev.Type), Type: string(dev.Type),
@ -154,6 +155,7 @@ func (device *VFIODevice) Dump() persistapi.DeviceState {
SysfsDev: dev.SysfsDev, SysfsDev: dev.SysfsDev,
}) })
} }
}
return ds return ds
} }

View File

@ -98,9 +98,6 @@ func (fs *FS) ToDisk() (retErr error) {
} }
if err := fs.lock(); err != nil { if err := fs.lock(); err != nil {
if err1 := fs.Destroy(); err1 != nil {
fs.Logger().WithError(err1).Errorf("failed to destroy dirs")
}
return err return err
} }
defer fs.unlock() defer fs.unlock()