Merge pull request #4092 from filecoin-project/fix/finalize-uns-panic

sectorstorage: Fix potential panic in FinalizeSector
This commit is contained in:
Łukasz Magiera 2020-09-30 09:18:01 +02:00 committed by GitHub
commit 4e8ccb2158
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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)