mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-11 06:18:27 +01:00
* storage: fail on short super block extra read even without an error Some backends (e.g. s3_backend) clear io.EOF after a short read, returning n < len(p) with a nil error. Checking n only when the error is non-nil lets truncated extra data reach proto.Unmarshal with a zero-filled tail. * storage: apply the same short-read check to the super block header A backend that returns n < SuperBlockSize with a nil error previously slipped through and parsed a partially zero-filled header. * storage: test super block extra reads Covers a round trip through ReadSuperBlock, a truncated extra region, a backend returning io.EOF alongside a full read, and a backend returning a short read with a nil error. --------- Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
56 lines
1.6 KiB
Go
56 lines
1.6 KiB
Go
package super_block
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/backend"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
)
|
|
|
|
// ReadSuperBlock reads from data file and load it into volume's super block
|
|
func ReadSuperBlock(datBackend backend.BackendStorageFile) (superBlock SuperBlock, err error) {
|
|
|
|
header := make([]byte, SuperBlockSize)
|
|
if n, e := datBackend.ReadAt(header, 0); n != SuperBlockSize {
|
|
if e == nil {
|
|
e = io.ErrUnexpectedEOF
|
|
}
|
|
err = fmt.Errorf("cannot read volume %s super block: %v", datBackend.Name(), e)
|
|
return
|
|
}
|
|
|
|
superBlock.Version = needle.Version(header[0])
|
|
if superBlock.ReplicaPlacement, err = NewReplicaPlacementFromByte(header[1]); err != nil {
|
|
err = fmt.Errorf("cannot read replica type: %s", err.Error())
|
|
return
|
|
}
|
|
superBlock.Ttl = needle.LoadTTLFromBytes(header[2:4])
|
|
superBlock.CompactionRevision = util.BytesToUint16(header[4:6])
|
|
superBlock.ExtraSize = util.BytesToUint16(header[6:8])
|
|
|
|
if superBlock.ExtraSize > 0 {
|
|
// read more
|
|
extraData := make([]byte, int(superBlock.ExtraSize))
|
|
if n, e := datBackend.ReadAt(extraData, SuperBlockSize); n != len(extraData) {
|
|
if e == nil {
|
|
e = io.ErrUnexpectedEOF
|
|
}
|
|
err = fmt.Errorf("cannot read volume %s super block extra: %v", datBackend.Name(), e)
|
|
return
|
|
}
|
|
superBlock.Extra = &master_pb.SuperBlockExtra{}
|
|
err = proto.Unmarshal(extraData, superBlock.Extra)
|
|
if err != nil {
|
|
err = fmt.Errorf("cannot unmarshal volume %s super block extra: %v", datBackend.Name(), err)
|
|
return
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|