Fix: Fail fast on unsupported volume versions (#8047)

* Fix: Fail fast when initializing volume with Version 0

* Fix: Fail fast when loading unsupported volume version (e.g. 0 or 4)

* Refactor: Use IsSupportedVersion helper function for version validation
This commit is contained in:
Chris Lu
2026-01-16 19:19:18 -08:00
committed by GitHub
parent 0a46577700
commit a473278bfa
3 changed files with 10 additions and 0 deletions

View File

@@ -11,3 +11,7 @@ const (
func GetCurrentVersion() Version {
return Version3
}
func IsSupportedVersion(v Version) bool {
return v >= Version1 && v <= Version3
}

View File

@@ -106,6 +106,9 @@ func (v *Volume) load(alsoLoadIndex bool, createDatIfMissing bool, needleMapKind
if alreadyHasSuperBlock {
err = v.readSuperBlock()
if err == nil {
if !needle.IsSupportedVersion(v.SuperBlock.Version) {
glog.Fatalf("Unsupported volume %d version %v", v.Id, v.SuperBlock.Version)
}
v.volumeInfo.Version = uint32(v.SuperBlock.Version)
}
glog.V(2).Infof("readSuperBlock volume %d version %v", v.Id, v.SuperBlock.Version)

View File

@@ -18,6 +18,9 @@ func (v *Volume) maybeWriteSuperBlock(ver needle.Version) error {
return e
}
if datSize == 0 {
if !needle.IsSupportedVersion(ver) {
return fmt.Errorf("volume super block version %d is not supported", ver)
}
v.SuperBlock.Version = ver
_, e = v.DataBackend.WriteAt(v.SuperBlock.Bytes(), 0)
if e != nil && os.IsPermission(e) {