Commit 8ba6deb1 authored by Russ Cox's avatar Russ Cox

compress/flate: fix infinite loop on malformed data

Test using compress/gzip, because that's how the
data arrived.

Fixes #6550.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/14441051
parent 158c56ef
......@@ -644,6 +644,10 @@ func (f *decompressor) huffSym(h *huffmanDecoder) (int, error) {
if n > huffmanChunkBits {
chunk = h.links[chunk>>huffmanValueShift][(f.b>>huffmanChunkBits)&h.linkMask]
n = uint(chunk & huffmanCountMask)
if n == 0 {
f.err = CorruptInputError(f.roffset)
return 0, f.err
}
}
if n <= f.nb {
f.b >>= n
......
......@@ -7,7 +7,10 @@ package gzip
import (
"bytes"
"io"
"io/ioutil"
"os"
"testing"
"time"
)
type gunzipTest struct {
......@@ -302,3 +305,31 @@ func TestDecompressor(t *testing.T) {
}
}
}
func TestIssue6550(t *testing.T) {
f, err := os.Open("testdata/issue6550.gz")
if err != nil {
t.Fatal(err)
}
gzip, err := NewReader(f)
if err != nil {
t.Fatalf("NewReader(testdata/issue6550.gz): %v", err)
}
defer gzip.Close()
done := make(chan bool, 1)
go func() {
_, err := io.Copy(ioutil.Discard, gzip)
if err == nil {
t.Errorf("Copy succeeded")
} else {
t.Logf("Copy failed (correctly): %v", err)
}
done <- true
}()
select {
case <-time.After(1 * time.Second):
t.Errorf("Copy hung")
case <-done:
// ok
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment