Skip to content

Commit

Permalink
Encoder: check for empty block
Browse files Browse the repository at this point in the history
Fixes #51
  • Loading branch information
andybalholm committed Jul 29, 2024
1 parent 97e8583 commit 57434b5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
44 changes: 44 additions & 0 deletions brotli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import (
"math"
"math/rand"
"os"
"strconv"
"testing"
"time"

"github.com/andybalholm/brotli/matchfinder"
"github.com/xyproto/randomstring"
)

func checkCompressedData(compressedData, wantOriginalData []byte) error {
Expand Down Expand Up @@ -740,3 +742,45 @@ func TestEncodeM0Lazy(t *testing.T) {
func BenchmarkEncodeM0Lazy(b *testing.B) {
benchmark(b, "testdata/Isaac.Newton-Opticks.txt", matchfinder.M0{Lazy: true}, 1<<16)
}

func TestIssue51(t *testing.T) {
for i := 65536; i <= 65536*4; i += 65536 {
t.Run("compress data length: "+strconv.Itoa(i)+"bytes", func(t *testing.T) {
dataStr := randomstring.HumanFriendlyString(i)
dataBytes := []byte(dataStr)
buf := bytes.Buffer{}
w := NewWriterV2(&buf, 4)

n, err := w.Write(dataBytes)
if err != nil {
t.Fatalf("Error while compressing data: %v", err)
}
if n != len(dataBytes) {
t.Fatalf("Bytes written (%d) != len(databytes) (%d)", n, len(dataBytes))
}
err = w.Close()
if err != nil {
t.Fatalf("Error closing writer: %v", err)
}

r := NewReader(&buf)
dst := make([]byte, len(dataBytes)+100)
p := dst
total := 0
for {
n1, err1 := r.Read(p)
if err1 != nil {
if err1 != io.EOF {
t.Fatal(err1)
}
break
}
total += n1
p = p[n1:]
}
if !bytes.Equal(dst[:total], dataBytes) {
t.Fatal("Decompressed bytes don't match")
}
})
}
}
9 changes: 9 additions & 0 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ func (e *Encoder) Encode(dst []byte, src []byte, matches []matchfinder.Match, la
e.wroteHeader = true
}

if len(src) == 0 {
if lastBlock {
e.bw.writeBits(2, 3) // islast + isempty
e.bw.jumpToByteBoundary()
return e.bw.dst
}
return dst
}

var literalHisto [256]uint32
var commandHisto [704]uint32
var distanceHisto [64]uint32
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ module github.com/andybalholm/brotli
go 1.13

retract v1.0.1 // occasional panics and data corruption

require github.com/xyproto/randomstring v1.0.5 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU=
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=

0 comments on commit 57434b5

Please sign in to comment.