1
0
mirror of https://github.com/golang/go synced 2024-11-17 05:54:46 -07:00

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
This commit is contained in:
Ehren Kret 2013-09-06 15:09:42 -07:00 committed by Brad Fitzpatrick
parent 187241582e
commit 3b6b53f493
2 changed files with 17 additions and 0 deletions

View 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.")
}
}

View 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