diff --git a/seaweed-block/core/storage/chunk_lsn_map_v3.go b/seaweed-block/core/storage/chunk_lsn_map_v3.go index 155c2d76f6..8414efe0e2 100644 --- a/seaweed-block/core/storage/chunk_lsn_map_v3.go +++ b/seaweed-block/core/storage/chunk_lsn_map_v3.go @@ -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) { diff --git a/seaweed-block/core/storage/chunk_lsn_map_v3_test.go b/seaweed-block/core/storage/chunk_lsn_map_v3_test.go index 5b6741ae52..37d3e62d63 100644 --- a/seaweed-block/core/storage/chunk_lsn_map_v3_test.go +++ b/seaweed-block/core/storage/chunk_lsn_map_v3_test.go @@ -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") + } +}