test: refuse map counters before allocation or I/O with typed exhaustion

This commit is contained in:
pingqiu committed 2026-11-17 02:23:36 -08:00
1 parent d2a778f849
commit 2a658d5dcf
2 files changed
+41 -3

No files matched your search

+12 -3
View File
@@ -68,7 +68,10 @@ func mapDigestV3(domain string, b []byte) [32]byte {
return out
}
func encodeMapPageV3(d descriptorV3, g, index uint64, es []chunkLSNEntryV3) ([]byte, error) {
if g == 0 || g > counterMaxV3 || len(es) == 0 || len(es) > 250 {
if g > counterMaxV3 {
return nil, ErrCounterExhausted
}
if g == 0 || len(es) == 0 || len(es) > 250 {
return nil, ErrMapInvalid
}
b := make([]byte, 4096)
@@ -98,7 +101,10 @@ func encodeMapRootV3(d descriptorV3, g, cb, C uint64, refs []mapPageRefV3) ([]by
if e != nil {
return nil, e
}
if g == 0 || g > counterMaxV3 || C > counterMaxV3 || uint64(len(refs)) != s.pages {
if g > counterMaxV3 || C > counterMaxV3 {
return nil, ErrCounterExhausted
}
if g == 0 || uint64(len(refs)) != s.pages {
return nil, ErrMapInvalid
}
b := make([]byte, int(s.aligned))
@@ -289,7 +295,10 @@ func (m *chunkLSNMapV3) Prepare(g, C uint64, entries []chunkLSNEntryV3, prior *c
if e != nil {
return nil, e
}
if uint64(len(entries)) != s.chunks || g == 0 || g <= m.lastGeneration || g > counterMaxV3 || C > counterMaxV3 {
if g > counterMaxV3 || C > counterMaxV3 {
return nil, ErrCounterExhausted
}
if uint64(len(entries)) != s.chunks || g == 0 || g <= m.lastGeneration {
return nil, ErrMapInvalid
}
if prior != nil && (prior.owner != m || prior.generation >= g) {
@@ -323,3 +323,32 @@ func TestChunkLSNMapV3_RejectsRehashedMalformedRecords(t *testing.T) {
})
}
}
func TestChunkLSNMapV3_CounterAndGeometryRefusal(t *testing.T) {
d := mapDescriptorV3(t)
if _, e := encodeMapPageV3(d, ^uint64(0), 0, []chunkLSNEntryV3{{}}); !errors.Is(e, ErrCounterExhausted) {
t.Fatalf("page counter=%v", e)
}
if _, e := encodeMapRootV3(d, 1, 1, ^uint64(0), []mapPageRefV3{{}}); !errors.Is(e, ErrCounterExhausted) {
t.Fatalf("root counter=%v", e)
}
m, e := createChunkLSNMapV3(filepath.Join(t.TempDir(), "map"), d, 1)
if e != nil {
t.Fatal(e)
}
defer m.Close()
if _, e = m.Prepare(^uint64(0), 7, []chunkLSNEntryV3{{}}, nil); !errors.Is(e, ErrCounterExhausted) {
t.Fatalf("prepare counter=%v", e)
}
stat, _ := m.fd.Stat()
if stat.Size() != 0 {
t.Fatal("invalid counter wrote bytes")
}
if _, e = mapShapeForV3(d, 0); e == nil {
t.Fatal("zero chunk geometry accepted")
}
d.VolumeSize++
if _, e = mapShapeForV3(d, 1); e == nil {
t.Fatal("nonintegral block geometry accepted")
}
}