From 621361cf88e09a51be16ac6cf3c9bc755fd58070 Mon Sep 17 00:00:00 2001 From: Lorenzo Torracchi Date: Thu, 19 Nov 2026 17:54:21 +0100 Subject: [PATCH] squashfs: readMetadata improve memory usage allocate only size bytes instead of whole block --- filesystem/squashfs/metadatablock.go | 19 +++--- .../squashfs/metadatablock_internal_test.go | 59 +++++++++++++++++++ filesystem/squashfs/squashfs.go | 4 -- 3 files changed, 69 insertions(+), 13 deletions(-) diff --git a/filesystem/squashfs/metadatablock.go b/filesystem/squashfs/metadatablock.go index 9ac03e98..5b41eca7 100644 --- a/filesystem/squashfs/metadatablock.go +++ b/filesystem/squashfs/metadatablock.go @@ -106,19 +106,20 @@ func (fs *FileSystem) readMetaBlock(r io.ReaderAt, c Compressor, location int64) // readMetadata read as many bytes of metadata as required for the given size, with the byteOffset provided as a starting // point into the first block. Can read multiple blocks if necessary, e.g. if a block is 8192 bytes (standard), and // requests to read 500 bytes beginning at offset 8000 into the first block. -// it always returns to the end of the block, even if that is greater than the given size. This makes it easy to use more -// data than expected on first read. The consumer is expected to cut it down, if needed +// It returns exactly size bytes, in a new slice the caller may keep or modify: copying up to the end of the block +// instead would cost up to a whole block per call, e.g. for every inode of a directory being listed. func (fs *FileSystem) readMetadata(r io.ReaderAt, c Compressor, firstBlock int64, initialBlockOffset uint32, byteOffset uint16, size int) ([]byte, error) { - var ( - b []byte - blockOffset = int(initialBlockOffset) - ) - // we know how many blocks, so read them all in + blockOffset := int(initialBlockOffset) m, read, err := fs.readMetaBlock(r, c, firstBlock+int64(blockOffset)) if err != nil { return nil, err } - b = append(b, m[byteOffset:]...) + if int(byteOffset) > len(m) { + return nil, fmt.Errorf("offset %d is beyond the %d bytes of metadata block at %d", byteOffset, len(m), firstBlock+int64(blockOffset)) + } + m = m[byteOffset:] + b := make([]byte, 0, size) + b = append(b, m[:min(len(m), size)]...) // do we have any more to read? for len(b) < size { blockOffset += int(read) @@ -126,7 +127,7 @@ func (fs *FileSystem) readMetadata(r io.ReaderAt, c Compressor, firstBlock int64 if err != nil { return nil, err } - b = append(b, m...) + b = append(b, m[:min(len(m), size-len(b))]...) } return b, nil } diff --git a/filesystem/squashfs/metadatablock_internal_test.go b/filesystem/squashfs/metadatablock_internal_test.go index cb597914..1d068650 100644 --- a/filesystem/squashfs/metadatablock_internal_test.go +++ b/filesystem/squashfs/metadatablock_internal_test.go @@ -156,3 +156,62 @@ func TestReadMetaBlockLimit(t *testing.T) { t.Errorf("%d bytes: expected %v, got %v", metadataBlockSize+1, ErrDecompressedTooLarge, err) } } + +func TestReadMetadata(t *testing.T) { + // three uncompressed metadata blocks, of 5, 4 and 3 bytes, at 0, 7 and 13 + table := []byte{ + 0x5, 0x80, 1, 2, 3, 4, 5, + 0x4, 0x80, 6, 7, 8, 9, + 0x3, 0x80, 10, 11, 12, + } + tests := []struct { + name string + blockOffset uint32 + byteOffset uint16 + size int + out []byte + err string + }{ + {"within the first block", 0, 1, 2, []byte{2, 3}, ""}, + {"to the end of the first block", 0, 0, 5, []byte{1, 2, 3, 4, 5}, ""}, + {"across two blocks", 0, 3, 4, []byte{4, 5, 6, 7}, ""}, + {"across three blocks", 0, 4, 6, []byte{5, 6, 7, 8, 9, 10}, ""}, + {"from a later block", 7, 1, 2, []byte{7, 8}, ""}, + {"from the end of a block", 0, 5, 2, []byte{6, 7}, ""}, + {"nothing", 0, 2, 0, []byte{}, ""}, + {"offset beyond the block", 0, 6, 1, nil, "offset 6 is beyond the 5 bytes of metadata block at 0"}, + {"beyond the end of the table", 7, 0, 20, nil, "metadata block at 18 compressed, but no compressor provided"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + fs := &FileSystem{cache: newLRU(10)} + b, err := fs.readMetadata(bytes.NewReader(table), nil, 0, tt.blockOffset, tt.byteOffset, tt.size) + switch { + case tt.err != "" && (err == nil || !strings.Contains(err.Error(), tt.err)): + t.Errorf("expected error %q, got %v", tt.err, err) + case tt.err == "" && err != nil: + t.Errorf("unexpected error: %v", err) + case !bytes.Equal(b, tt.out): + t.Errorf("got % x, expected % x", b, tt.out) + } + }) + } + + // the bytes returned are the caller's own: changing them does not change what the cache returns next + t.Run("returns a copy", func(t *testing.T) { + fs := &FileSystem{cache: newLRU(10)} + b, err := fs.readMetadata(bytes.NewReader(table), nil, 0, 0, 0, 5) + if err != nil { + t.Fatal(err) + } + b[0] = 0xff + b, err = fs.readMetadata(bytes.NewReader(table), nil, 0, 0, 0, 5) + if err != nil { + t.Fatal(err) + } + if b[0] != 1 { + t.Errorf("cached block changed through a returned slice: got % x", b) + } + }) +} diff --git a/filesystem/squashfs/squashfs.go b/filesystem/squashfs/squashfs.go index c904dacd..84c1a015 100644 --- a/filesystem/squashfs/squashfs.go +++ b/filesystem/squashfs/squashfs.go @@ -754,10 +754,6 @@ func (fs *FileSystem) getDirectory(blockOffset uint32, byteOffset uint16, size i if err != nil { return nil, fmt.Errorf("error reading block at position %d: %v", blockOffset, err) } - // for parseDirectory, we only want to use precisely the right number of bytes - if len(uncompressed) > size { - uncompressed = uncompressed[:size] - } // get the inode from the offset into the uncompressed block return parseDirectory(uncompressed) }