sectorstorage: Fix potential panic in FinalizeSector

This commit is contained in:
Łukasz Magiera 2020-09-29 15:22:46 +02:00
parent 867ca2f25d
commit baef3c8dd2

View File

@ -546,34 +546,37 @@ func (sb *Sealer) FinalizeSector(ctx context.Context, sector abi.SectorID, keepU
defer done() defer done()
pf, err := openPartialFile(maxPieceSize, paths.Unsealed) pf, err := openPartialFile(maxPieceSize, paths.Unsealed)
if xerrors.Is(err, os.ErrNotExist) { if err == nil {
return xerrors.Errorf("opening partial file: %w", err) var at uint64
} for sr.HasNext() {
r, err := sr.NextRun()
if err != nil {
_ = pf.Close()
return err
}
var at uint64 offset := at
for sr.HasNext() { at += r.Len
r, err := sr.NextRun() if !r.Val {
if err != nil { continue
_ = pf.Close() }
err = pf.Free(storiface.PaddedByteIndex(abi.UnpaddedPieceSize(offset).Padded()), abi.UnpaddedPieceSize(r.Len).Padded())
if err != nil {
_ = pf.Close()
return xerrors.Errorf("free partial file range: %w", err)
}
}
if err := pf.Close(); err != nil {
return err return err
} }
} else {
offset := at if !xerrors.Is(err, os.ErrNotExist) {
at += r.Len return xerrors.Errorf("opening partial file: %w", err)
if !r.Val {
continue
}
err = pf.Free(storiface.PaddedByteIndex(abi.UnpaddedPieceSize(offset).Padded()), abi.UnpaddedPieceSize(r.Len).Padded())
if err != nil {
_ = pf.Close()
return xerrors.Errorf("free partial file range: %w", err)
} }
} }
if err := pf.Close(); err != nil {
return err
}
} }
paths, done, err := sb.sectors.AcquireSector(ctx, sector, stores.FTCache, 0, stores.PathStorage) paths, done, err := sb.sectors.AcquireSector(ctx, sector, stores.FTCache, 0, stores.PathStorage)