mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 16:33:21 +01:00
fix: re-use embedded union reader if possible (#2814)
* fix: re-use embedded union reader if possible Previously, because file.LocationReadCloser embeds a ReadCloser that might be a UnionReader, but doesn't implement the interface itself, the type assertion would fall and Syft would fall back to io.ReadAll to enable seeking on the underlying reader, resulting in a potentially large extra allocation. Instead, check whether the passed ReadCloser is a file.LocationReadCloser, and if so, try to use the embedded ReadCloser as a UnionReader. Signed-off-by: Will Murphy <will.murphy@anchore.com> * lint fix Signed-off-by: Will Murphy <will.murphy@anchore.com> * Assert that underlying reader is returned Signed-off-by: Will Murphy <will.murphy@anchore.com> --------- Signed-off-by: Will Murphy <will.murphy@anchore.com>
This commit is contained in:
parent
8640f978ba
commit
d3310a1830
@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
macho "github.com/anchore/go-macholibre"
|
macho "github.com/anchore/go-macholibre"
|
||||||
"github.com/anchore/syft/internal/log"
|
"github.com/anchore/syft/internal/log"
|
||||||
|
"github.com/anchore/syft/syft/file"
|
||||||
)
|
)
|
||||||
|
|
||||||
// UnionReader is a single interface with all reading functions needed by multi-arch binary catalogers
|
// UnionReader is a single interface with all reading functions needed by multi-arch binary catalogers
|
||||||
@ -44,6 +45,17 @@ func GetUnionReader(readerCloser io.ReadCloser) (UnionReader, error) {
|
|||||||
return reader, nil
|
return reader, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// file.LocationReadCloser embeds a ReadCloser, which is likely
|
||||||
|
// to implement UnionReader. Check whether the embedded read closer
|
||||||
|
// implements UnionReader, and just return that if so.
|
||||||
|
r, ok := readerCloser.(file.LocationReadCloser)
|
||||||
|
if ok {
|
||||||
|
ur, ok := r.ReadCloser.(UnionReader)
|
||||||
|
if ok {
|
||||||
|
return ur, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
b, err := io.ReadAll(readerCloser)
|
b, err := io.ReadAll(readerCloser)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to read contents from binary: %w", err)
|
return nil, fmt.Errorf("unable to read contents from binary: %w", err)
|
||||||
|
|||||||
@ -7,6 +7,8 @@ import (
|
|||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/anchore/syft/syft/file"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_getUnionReader_notUnionReader(t *testing.T) {
|
func Test_getUnionReader_notUnionReader(t *testing.T) {
|
||||||
@ -28,3 +30,34 @@ func Test_getUnionReader_notUnionReader(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(t, expectedContents, string(b))
|
assert.Equal(t, expectedContents, string(b))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type panickingUnionReader struct{}
|
||||||
|
|
||||||
|
func (p2 *panickingUnionReader) ReadAt(p []byte, off int64) (n int, err error) {
|
||||||
|
panic("don't call this in your unit test!")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p2 *panickingUnionReader) Seek(offset int64, whence int) (int64, error) {
|
||||||
|
panic("don't call this in your unit test!")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p2 *panickingUnionReader) Read(p []byte) (n int, err error) {
|
||||||
|
panic("don't call this in your unit test!")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p2 *panickingUnionReader) Close() error {
|
||||||
|
panic("don't call this in your unit test!")
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ UnionReader = (*panickingUnionReader)(nil)
|
||||||
|
|
||||||
|
func Test_getUnionReader_fileLocationReadCloser(t *testing.T) {
|
||||||
|
// panickingUnionReader is a UnionReader
|
||||||
|
p := &panickingUnionReader{}
|
||||||
|
embedsUnionReader := file.NewLocationReadCloser(file.Location{}, p)
|
||||||
|
|
||||||
|
// embedded union reader is returned without "ReadAll" invocation
|
||||||
|
ur, err := GetUnionReader(embedsUnionReader)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, p, ur)
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user