From: Ehren Kret Date: Fri, 6 Sep 2013 22:09:42 +0000 (-0700) Subject: compress/flate: prevent panic when reinitializing huffmanDecoder with bad input X-Git-Tag: go1.2rc2~319 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=3b6b53f493af5984ca50cc202c3f8e155ad6f526;p=gostls13.git compress/flate: prevent panic when reinitializing huffmanDecoder with bad input The huffmanDecoder struct appears to be intented for reuse by calling init a second time with a second sequence of code lengths. Unfortunately, it can currently panic if the second sequence of code lengths has a minimum value greater than 10 due to failure to reinitialize the links table. This change prevents the panic by resetting the huffmanDecoder struct back to the struct's zero value at the beginning of the init method if the huffmanDecoder is being reused (determined by checking if min has been set to a non-zero value). Fixes #6255. R=golang-dev, rsc CC=golang-dev https://golang.org/cl/13230043 --- diff --git a/src/pkg/compress/flate/flate_test.go b/src/pkg/compress/flate/flate_test.go index 60584ec58f..57fea5ab4d 100644 --- a/src/pkg/compress/flate/flate_test.go +++ b/src/pkg/compress/flate/flate_test.go @@ -47,3 +47,16 @@ func TestIssue5962(t *testing.T) { t.Fatalf("Given sequence of bits is bad, and should not succeed.") } } + +// The following test should not panic. +func TestIssue6255(t *testing.T) { + bits1 := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11} + bits2 := []int{11, 13} + h := new(huffmanDecoder) + if !h.init(bits1) { + t.Fatalf("Given sequence of bits is good and should succeed.") + } + if h.init(bits2) { + t.Fatalf("Given sequence of bits is bad and should not succeed.") + } +} diff --git a/src/pkg/compress/flate/inflate.go b/src/pkg/compress/flate/inflate.go index 0287867208..34ba00d5af 100644 --- a/src/pkg/compress/flate/inflate.go +++ b/src/pkg/compress/flate/inflate.go @@ -91,6 +91,10 @@ type huffmanDecoder struct { // Initialize Huffman decoding tables from array of code lengths. func (h *huffmanDecoder) init(bits []int) bool { + if h.min != 0 { + *h = huffmanDecoder{} + } + // Count number of codes of each length, // compute min and max length. var count [maxCodeLen]int