mirror of
https://github.com/anchore/syft.git
synced 2025-11-18 08:53:15 +01:00
more passing lazy union reader tests
Signed-off-by: Will Murphy <will.murphy@anchore.com>
This commit is contained in:
parent
20a26a0dfe
commit
bad5cf2af8
@ -49,8 +49,12 @@ func (c *lazyUnionReader) ReadAt(p []byte, off int64) (n int, err error) {
|
|||||||
needUntil := int64(len(p)) + off
|
needUntil := int64(len(p)) + off
|
||||||
err = c.ensureReadUntil(needUntil)
|
err = c.ensureReadUntil(needUntil)
|
||||||
end := min(off+int64(len(p)), int64(len(c.buf)))
|
end := min(off+int64(len(p)), int64(len(c.buf)))
|
||||||
copy(p, c.buf[off:end])
|
start := min(off, c.maxRead())
|
||||||
return int(end - off), err
|
if off > start {
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
copy(p, c.buf[start:end])
|
||||||
|
return int(end - start), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *lazyUnionReader) Seek(offset int64, whence int) (int64, error) {
|
func (c *lazyUnionReader) Seek(offset int64, whence int) (int64, error) {
|
||||||
@ -63,6 +67,7 @@ func (c *lazyUnionReader) Seek(offset int64, whence int) (int64, error) {
|
|||||||
trueOffset = offset
|
trueOffset = offset
|
||||||
case io.SeekCurrent:
|
case io.SeekCurrent:
|
||||||
trueOffset = offset + c.cursor
|
trueOffset = offset + c.cursor
|
||||||
|
err = c.ensureReadUntil(trueOffset)
|
||||||
case io.SeekEnd:
|
case io.SeekEnd:
|
||||||
err = c.readAll()
|
err = c.readAll()
|
||||||
trueOffset = c.maxRead() + offset
|
trueOffset = c.maxRead() + offset
|
||||||
@ -73,6 +78,7 @@ func (c *lazyUnionReader) Seek(offset int64, whence int) (int64, error) {
|
|||||||
if trueOffset < 0 {
|
if trueOffset < 0 {
|
||||||
return 0, fmt.Errorf("request to read negative offset impossible %v", trueOffset)
|
return 0, fmt.Errorf("request to read negative offset impossible %v", trueOffset)
|
||||||
}
|
}
|
||||||
|
trueOffset = min(c.maxRead(), trueOffset)
|
||||||
c.cursor = trueOffset
|
c.cursor = trueOffset
|
||||||
return c.cursor, nil
|
return c.cursor, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -101,6 +101,21 @@ func Test_lazyUnionReader_ReadAt(t *testing.T) {
|
|||||||
wantBytes: []byte("ef"),
|
wantBytes: []byte("ef"),
|
||||||
wantEOF: true,
|
wantEOF: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "read way out of bounds",
|
||||||
|
dst: make([]byte, 4),
|
||||||
|
off: 512,
|
||||||
|
wantN: 0,
|
||||||
|
wantEOF: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "buffer more than available",
|
||||||
|
dst: make([]byte, 512),
|
||||||
|
off: 0,
|
||||||
|
wantN: 16,
|
||||||
|
wantBytes: []byte("0123456789abcdef"),
|
||||||
|
wantEOF: true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user