Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reset overflow #218

Merged
merged 5 commits into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions flate/fast_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ const (
baseMatchLength = 3 // The smallest match length per the RFC section 3.2.5
maxMatchOffset = 1 << 15 // The largest match offset

bTableBits = 18 // Bits used in the big tables
bTableSize = 1 << bTableBits // Size of the table
allocHistory = maxMatchOffset * 10 // Size to preallocate for history.
bufferReset = (1 << 31) - allocHistory - maxStoreBlockSize // Reset the buffer offset when reaching this.
bTableBits = 18 // Bits used in the big tables
bTableSize = 1 << bTableBits // Size of the table
allocHistory = maxStoreBlockSize * 20 // Size to preallocate for history.
bufferReset = (1 << 31) - allocHistory - maxStoreBlockSize - 1 // Reset the buffer offset when reaching this.
)

const (
Expand Down Expand Up @@ -210,16 +210,14 @@ func (e *fastGen) matchlenLong(s, t int32, src []byte) int32 {

// Reset the encoding table.
func (e *fastGen) Reset() {
if cap(e.hist) < int(maxMatchOffset*8) {
l := maxMatchOffset * 8
// Make it at least 1MB.
if l < 1<<20 {
l = 1 << 20
}
e.hist = make([]byte, 0, l)
if cap(e.hist) < allocHistory {
e.hist = make([]byte, 0, allocHistory)
}
// We offset current position so everything will be out of reach.
// If we are above the buffer reset it will be cleared anyway since len(hist) == 0.
if e.cur <= bufferReset {
e.cur += maxMatchOffset + int32(len(e.hist))
}
// We offset current position so everything will be out of reach
e.cur += maxMatchOffset + int32(len(e.hist))
e.hist = e.hist[:0]
}

Expand Down
5 changes: 5 additions & 0 deletions flate/level1.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package flate

import "fmt"

// fastGen maintains the table for matches,
// and the previous byte block for level 2.
// This is the generic implementation.
Expand All @@ -14,6 +16,9 @@ func (e *fastEncL1) Encode(dst *tokens, src []byte) {
inputMargin = 12 - 1
minNonLiteralBlockSize = 1 + 1 + inputMargin
)
if debugDecode && e.cur < 0 {
panic(fmt.Sprint("e.cur < 0: ", e.cur))
}

// Protect against e.cur wraparound.
for e.cur >= bufferReset {
Expand Down
6 changes: 6 additions & 0 deletions flate/level2.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package flate

import "fmt"

// fastGen maintains the table for matches,
// and the previous byte block for level 2.
// This is the generic implementation.
Expand All @@ -16,6 +18,10 @@ func (e *fastEncL2) Encode(dst *tokens, src []byte) {
minNonLiteralBlockSize = 1 + 1 + inputMargin
)

if debugDecode && e.cur < 0 {
panic(fmt.Sprint("e.cur < 0: ", e.cur))
}

// Protect against e.cur wraparound.
for e.cur >= bufferReset {
if len(e.hist) == 0 {
Expand Down
6 changes: 6 additions & 0 deletions flate/level3.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package flate

import "fmt"

// fastEncL3
type fastEncL3 struct {
fastGen
Expand All @@ -13,6 +15,10 @@ func (e *fastEncL3) Encode(dst *tokens, src []byte) {
minNonLiteralBlockSize = 1 + 1 + inputMargin
)

if debugDecode && e.cur < 0 {
panic(fmt.Sprint("e.cur < 0: ", e.cur))
}

// Protect against e.cur wraparound.
for e.cur >= bufferReset {
if len(e.hist) == 0 {
Expand Down
4 changes: 3 additions & 1 deletion flate/level4.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ func (e *fastEncL4) Encode(dst *tokens, src []byte) {
inputMargin = 12 - 1
minNonLiteralBlockSize = 1 + 1 + inputMargin
)

if debugDecode && e.cur < 0 {
panic(fmt.Sprint("e.cur < 0: ", e.cur))
}
// Protect against e.cur wraparound.
for e.cur >= bufferReset {
if len(e.hist) == 0 {
Expand Down
3 changes: 3 additions & 0 deletions flate/level5.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ func (e *fastEncL5) Encode(dst *tokens, src []byte) {
inputMargin = 12 - 1
minNonLiteralBlockSize = 1 + 1 + inputMargin
)
if debugDecode && e.cur < 0 {
panic(fmt.Sprint("e.cur < 0: ", e.cur))
}

// Protect against e.cur wraparound.
for e.cur >= bufferReset {
Expand Down
3 changes: 3 additions & 0 deletions flate/level6.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ func (e *fastEncL6) Encode(dst *tokens, src []byte) {
inputMargin = 12 - 1
minNonLiteralBlockSize = 1 + 1 + inputMargin
)
if debugDecode && e.cur < 0 {
panic(fmt.Sprint("e.cur < 0: ", e.cur))
}

// Protect against e.cur wraparound.
for e.cur >= bufferReset {
Expand Down
70 changes: 70 additions & 0 deletions flate/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"io"
"io/ioutil"
"math"
"math/rand"
"runtime"
"strconv"
Expand Down Expand Up @@ -265,6 +266,75 @@ func TestWriteError(t *testing.T) {
}
}

// Test if errors from the underlying writer is passed upwards.
func TestWriter_Reset(t *testing.T) {
buf := new(bytes.Buffer)
n := 65536
if !testing.Short() {
n *= 4
}
for i := 0; i < n; i++ {
fmt.Fprintf(buf, "asdasfasf%d%dfghfgujyut%dyutyu\n", i, i, i)
}
in := buf.Bytes()
for l := 0; l < 10; l++ {
l := l
if testing.Short() && l > 1 {
continue
}
t.Run(fmt.Sprintf("level-%d", l), func(t *testing.T) {
t.Parallel()
offset := 1
if testing.Short() {
offset = 256
}
for ; offset <= 256; offset *= 2 {
// Fail after 'fail' writes
w, err := NewWriter(ioutil.Discard, l)
if err != nil {
t.Fatalf("NewWriter: level %d: %v", l, err)
}
if w.d.fast == nil {
t.Skip("Not Fast...")
return
}
for i := 0; i < (bufferReset-len(in)-offset-maxMatchOffset)/maxMatchOffset; i++ {
// skip ahead to where we are close to wrap around...
w.d.fast.Reset()
}
w.d.fast.Reset()
_, err = w.Write(in)
if err != nil {
t.Fatal(err)
}
for i := 0; i < 50; i++ {
// skip ahead again... This should wrap around...
w.d.fast.Reset()
}
w.d.fast.Reset()

_, err = w.Write(in)
if err != nil {
t.Fatal(err)
}
for i := 0; i < (math.MaxUint32-bufferReset)/maxMatchOffset; i++ {
// skip ahead to where we are close to wrap around...
w.d.fast.Reset()
}

_, err = w.Write(in)
if err != nil {
t.Fatal(err)
}
err = w.Close()
if err != nil {
t.Fatal(err)
}
}
})
}
}

func TestDeterministicL1(t *testing.T) { testDeterministic(1, t) }
func TestDeterministicL2(t *testing.T) { testDeterministic(2, t) }
func TestDeterministicL3(t *testing.T) { testDeterministic(3, t) }
Expand Down