diff --git a/weed/format/layout.go b/weed/format/layout.go index 1ba4aeb10b..c472635258 100644 --- a/weed/format/layout.go +++ b/weed/format/layout.go @@ -16,6 +16,8 @@ const ( MaxExtentCount = 1 << 20 // MaxPayloadBytes bounds the adapter payload carried in entry metadata. MaxPayloadBytes = 16 << 20 + // MaxFormatNameBytes bounds the encoded adapter name. + MaxFormatNameBytes = 256 ) // TotalSize returns the file size the layout describes. @@ -44,6 +46,9 @@ func (l *Layout) Validate(fileSize int64) error { if l.Format == "" { return fmt.Errorf("layout has no format name") } + if len(l.Format) > MaxFormatNameBytes { + return fmt.Errorf("layout format name is too long: %d bytes", len(l.Format)) + } if l.Align < 1 { return fmt.Errorf("layout align %d is invalid", l.Align) } @@ -97,7 +102,7 @@ func DecodeLayout(data []byte) (*Layout, error) { if err != nil || version != layoutVersion { return nil, fmt.Errorf("unsupported layout version") } - name, err := readUvarintBytes(reader, 256) + name, err := readUvarintBytes(reader, MaxFormatNameBytes) if err != nil { return nil, fmt.Errorf("read layout format: %w", err) } diff --git a/weed/format/layout_test.go b/weed/format/layout_test.go index 81b25e3a7c..5139b848fe 100644 --- a/weed/format/layout_test.go +++ b/weed/format/layout_test.go @@ -66,6 +66,7 @@ func TestLayoutValidate(t *testing.T) { {"no extents", Layout{Format: "x", Align: 1}, -1, "no extents"}, {"bad align", Layout{Format: "x", ExtentSizes: []int64{5}, Align: 0}, -1, "align"}, {"no name", Layout{ExtentSizes: []int64{5}, Align: 1}, -1, "format name"}, + {"name too long", Layout{Format: strings.Repeat("x", MaxFormatNameBytes+1), ExtentSizes: []int64{5}, Align: 1}, -1, "too long"}, } for _, test := range tests { err := test.layout.Validate(test.fileSize)