]> Cypherpunks repositories - gostls13.git/commitdiff
compress/flate: prevent panic when reinitializing huffmanDecoder with bad input
authorEhren Kret <ehren.kret@gmail.com>
Fri, 6 Sep 2013 22:09:42 +0000 (15:09 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Fri, 6 Sep 2013 22:09:42 +0000 (15:09 -0700)
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

src/pkg/compress/flate/flate_test.go
src/pkg/compress/flate/inflate.go

index 60584ec58fb4ffd006d8f1ed6bdd79d389729239..57fea5ab4dcfa65a207a60396827d510ae13962f 100644 (file)
@@ -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.")
+       }
+}
index 02878672084da434edf71d3957ae1561ebd36b37..34ba00d5af7c137f39d68e8822e583ad06afb787 100644 (file)
@@ -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