Skip to content

Commit

Permalink
fuzz found a bug
Browse files Browse the repository at this point in the history
  • Loading branch information
nikandfor committed Feb 10, 2025
1 parent 0b6b00c commit 74522a5
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
uses: golangci/golangci-lint-action@v2
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.59
version: v1.63.4

# Optional: working directory, useful for monorepos
# working-directory: somedir
Expand Down
30 changes: 23 additions & 7 deletions eazy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1311,11 +1311,17 @@ func FuzzWriter(f *testing.F) {
[]byte("aaaaaaaaaaabaaaaaaaaaaaa"),
)

var wbuf, rbuf bytes.Buffer
buf := make([]byte, 16)

w := NewWriter(nil, 512, 32)
r := NewReader(nil)

f.Fuzz(func(t *testing.T, p0, p1, p2 []byte) {
var wbuf, rbuf bytes.Buffer
buf := make([]byte, 16)
wbuf.Reset()
rbuf.Reset()

w := NewWriter(&wbuf, 512, 32)
w.Reset(&wbuf)

for _, p := range [][]byte{p0, p1, p2} {
n, err := w.Write(p)
Expand All @@ -1329,12 +1335,13 @@ func FuzzWriter(f *testing.F) {
return
}

t.Logf("input:\n%4x: %q\n%4x: %q\n%4x: %q", len(p0), p0, len(p1), p1, len(p2), p2)
t.Logf("encoded dump\n%s", Dump(wbuf.Bytes()))

panic(p)
}()

r := NewReaderBytes(wbuf.Bytes())
r.Reset(&wbuf)

m, err := io.CopyBuffer(&rbuf, r, buf)
assert.NoError(t, err)
Expand Down Expand Up @@ -1370,10 +1377,14 @@ func FuzzReader(f *testing.F) {
f.Add([]byte{Meta, MetaVer, 1, Meta, MetaReset, 6, Literal | 3, 'a', 'b', 'c'})
f.Add([]byte{Meta, MetaVer, 1, Meta, MetaReset, 6, Literal | 3, 'a', 'b', 'c', Copy | 3, 0})

var h, b BufReader
r := NewReader(nil)
w := NewDumper(nil)

f.Fuzz(func(t *testing.T, p []byte) {
b := BufReader{Buf: p}
r := NewReader(&b)
w := NewDumper(nil)
h.Reset(header)
b.Reset(p)
r.Reset(&b)

_, err := io.Copy(w, io.MultiReader(
&BufReader{Buf: header},
Expand Down Expand Up @@ -1503,6 +1514,11 @@ func (b *Buf) Write(p []byte) (int, error) {
return len(p), nil
}

func (b *BufReader) Reset(p []byte) {
b.Buf = p
b.R = 0
}

func (b *BufReader) Read(p []byte) (int, error) {
n := copy(p, b.Buf[b.R:])
b.R += n
Expand Down
5 changes: 4 additions & 1 deletion reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ func (r *Reader) read(p []byte, st int) (n, i int, err error) {
// ^ 0 ^ run ^ end // p pos

run := int(r.pos) - r.off
if run > len(p) {
run = len(p)
}

for j := 0; j < run; {
j += copy(p[j:run], r.block[(r.off+j)&r.mask:])
Expand Down Expand Up @@ -664,7 +667,7 @@ func (w *Dumper) Write(p []byte) (i int, err error) { //nolint:gocognit
w.r.d.Ver = int(p[i])
}

w.b = fmt.Appendf(w.b, "meta %2x %x %q\n", meta>>3, l, p[i:i+l])
w.b = fmt.Appendf(w.b, "meta %2x %x %-8q % [3]x\n", meta>>3, l, p[i:i+l])

if w.Debug != nil {
w.Debug(w.r.boff+int64(st), w.r.boff+int64(i), w.r.pos, 'm', l, meta)
Expand Down
2 changes: 2 additions & 0 deletions testdata/fuzz/FuzzReader/50a1dcccf68df51c
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("\x80\x10\x0f\x0f0 \x010\x00\x00\x00\x100000000\xd90\xd90\xd90\xd90\xd90\xd9_\xd9!ԁ\xbe\x83\xfe\xd90\xd9\x00\xff0\xd9_\xd9!")
4 changes: 4 additions & 0 deletions testdata/fuzz/FuzzWriter/aa21c93f4bd24cfa
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
go test fuzz v1
[]byte("0x\x00\x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
[]byte("00000")
[]byte("\x00x\x00\x00x\x00\x00x\x00")
2 changes: 1 addition & 1 deletion writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (w *Writer) init(bs, hs int) {
}

if (hs-1)&hs != 0 || hs < 4 {
panic("hash table size must be a power of two (hs > 4)")
panic("hash table size must be a power of two (hs >= 4)")
}

w.mask = bs - 1
Expand Down

0 comments on commit 74522a5

Please sign in to comment.