diff options
author | Ian Lance Taylor <ian@gcc.gnu.org> | 2014-06-06 22:37:27 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2014-06-06 22:37:27 +0000 |
commit | 6736ef96eab222e58e6294f42be981a5afb59811 (patch) | |
tree | 2bc668fae9bf96f9a3988e0b0a16685bde8c4f0b /libgo/go/compress | |
parent | 38a138411da4206c53f9a153ee9c3624fce58a52 (diff) | |
download | gcc-6736ef96eab222e58e6294f42be981a5afb59811.zip gcc-6736ef96eab222e58e6294f42be981a5afb59811.tar.gz gcc-6736ef96eab222e58e6294f42be981a5afb59811.tar.bz2 |
libgo: Merge to master revision 19184.
The next revision, 19185, renames several runtime files, and
will be handled in a separate change.
From-SVN: r211328
Diffstat (limited to 'libgo/go/compress')
-rw-r--r-- | libgo/go/compress/bzip2/bzip2_test.go | 10 | ||||
-rw-r--r-- | libgo/go/compress/bzip2/huffman.go | 32 | ||||
-rw-r--r-- | libgo/go/compress/flate/flate_test.go | 2 | ||||
-rw-r--r-- | libgo/go/compress/gzip/gunzip_test.go | 2 | ||||
-rw-r--r-- | libgo/go/compress/gzip/gzip_test.go | 2 | ||||
-rw-r--r-- | libgo/go/compress/lzw/reader_test.go | 4 | ||||
-rw-r--r-- | libgo/go/compress/zlib/reader_test.go | 2 |
7 files changed, 42 insertions, 12 deletions
diff --git a/libgo/go/compress/bzip2/bzip2_test.go b/libgo/go/compress/bzip2/bzip2_test.go index ada1f9a..cd647e5 100644 --- a/libgo/go/compress/bzip2/bzip2_test.go +++ b/libgo/go/compress/bzip2/bzip2_test.go @@ -14,7 +14,7 @@ import ( ) func TestBitReader(t *testing.T) { - buf := bytes.NewBuffer([]byte{0xaa}) + buf := bytes.NewReader([]byte{0xaa}) br := newBitReader(buf) if n := br.ReadBits(1); n != 1 { t.Errorf("read 1 wrong") @@ -31,7 +31,7 @@ func TestBitReader(t *testing.T) { } func TestBitReaderLarge(t *testing.T) { - buf := bytes.NewBuffer([]byte{0x12, 0x34, 0x56, 0x78}) + buf := bytes.NewReader([]byte{0x12, 0x34, 0x56, 0x78}) br := newBitReader(buf) if n := br.ReadBits(32); n != 0x12345678 { t.Errorf("got: %x want: %x", n, 0x12345678) @@ -43,7 +43,7 @@ func readerFromHex(s string) io.Reader { if err != nil { panic("readerFromHex: bad input") } - return bytes.NewBuffer(data) + return bytes.NewReader(data) } func decompressHex(s string) (out []byte, err error) { @@ -191,7 +191,7 @@ func benchmarkDecode(b *testing.B, testfile int) { } b.SetBytes(int64(len(compressed))) for i := 0; i < b.N; i++ { - r := bytes.NewBuffer(compressed) + r := bytes.NewReader(compressed) io.Copy(ioutil.Discard, NewReader(r)) } } @@ -201,7 +201,7 @@ func BenchmarkDecodeTwain(b *testing.B) { benchmarkDecode(b, twain) } func TestBufferOverrun(t *testing.T) { // Tests https://code.google.com/p/go/issues/detail?id=5747. - buffer := bytes.NewBuffer([]byte(bufferOverrunBase64)) + buffer := bytes.NewReader([]byte(bufferOverrunBase64)) decoder := base64.NewDecoder(base64.StdEncoding, buffer) decompressor := NewReader(decoder) // This shouldn't panic. diff --git a/libgo/go/compress/bzip2/huffman.go b/libgo/go/compress/bzip2/huffman.go index 8f6b0c9..75a6223 100644 --- a/libgo/go/compress/bzip2/huffman.go +++ b/libgo/go/compress/bzip2/huffman.go @@ -190,7 +190,37 @@ func buildHuffmanNode(t *huffmanTree, codes []huffmanCode, level uint32) (nodeIn right := codes[firstRightIndex:] if len(left) == 0 || len(right) == 0 { - return 0, StructuralError("superfluous level in Huffman tree") + // There is a superfluous level in the Huffman tree indicating + // a bug in the encoder. However, this bug has been observed in + // the wild so we handle it. + + // If this function was called recursively then we know that + // len(codes) >= 2 because, otherwise, we would have hit the + // "leaf node" case, below, and not recursed. + // + // However, for the initial call it's possible that len(codes) + // is zero or one. Both cases are invalid because a zero length + // tree cannot encode anything and a length-1 tree can only + // encode EOF and so is superfluous. We reject both. + if len(codes) < 2 { + return 0, StructuralError("empty Huffman tree") + } + + // In this case the recursion doesn't always reduce the length + // of codes so we need to ensure termination via another + // mechanism. + if level == 31 { + // Since len(codes) >= 2 the only way that the values + // can match at all 32 bits is if they are equal, which + // is invalid. This ensures that we never enter + // infinite recursion. + return 0, StructuralError("equal symbols in Huffman tree") + } + + if len(left) == 0 { + return buildHuffmanNode(t, right, level+1) + } + return buildHuffmanNode(t, left, level+1) } nodeIndex = uint16(t.nextNode) diff --git a/libgo/go/compress/flate/flate_test.go b/libgo/go/compress/flate/flate_test.go index 57fea5a..0687663 100644 --- a/libgo/go/compress/flate/flate_test.go +++ b/libgo/go/compress/flate/flate_test.go @@ -14,7 +14,7 @@ import ( ) func TestUncompressedSource(t *testing.T) { - decoder := NewReader(bytes.NewBuffer([]byte{0x01, 0x01, 0x00, 0xfe, 0xff, 0x11})) + decoder := NewReader(bytes.NewReader([]byte{0x01, 0x01, 0x00, 0xfe, 0xff, 0x11})) output := make([]byte, 1) n, error := decoder.Read(output) if n != 1 || error != nil { diff --git a/libgo/go/compress/gzip/gunzip_test.go b/libgo/go/compress/gzip/gunzip_test.go index 572fb58..5615373 100644 --- a/libgo/go/compress/gzip/gunzip_test.go +++ b/libgo/go/compress/gzip/gunzip_test.go @@ -284,7 +284,7 @@ var gunzipTests = []gunzipTest{ func TestDecompressor(t *testing.T) { b := new(bytes.Buffer) for _, tt := range gunzipTests { - in := bytes.NewBuffer(tt.gzip) + in := bytes.NewReader(tt.gzip) gzip, err := NewReader(in) if err != nil { t.Errorf("%s: NewReader: %s", tt.name, err) diff --git a/libgo/go/compress/gzip/gzip_test.go b/libgo/go/compress/gzip/gzip_test.go index 119be2e..09271b2 100644 --- a/libgo/go/compress/gzip/gzip_test.go +++ b/libgo/go/compress/gzip/gzip_test.go @@ -85,7 +85,7 @@ func TestRoundTrip(t *testing.T) { func TestLatin1(t *testing.T) { latin1 := []byte{0xc4, 'u', 0xdf, 'e', 'r', 'u', 'n', 'g', 0} utf8 := "Äußerung" - z := Reader{r: bufio.NewReader(bytes.NewBuffer(latin1))} + z := Reader{r: bufio.NewReader(bytes.NewReader(latin1))} s, err := z.readString() if err != nil { t.Fatalf("readString: %v", err) diff --git a/libgo/go/compress/lzw/reader_test.go b/libgo/go/compress/lzw/reader_test.go index 6f155b1..9006c91 100644 --- a/libgo/go/compress/lzw/reader_test.go +++ b/libgo/go/compress/lzw/reader_test.go @@ -127,7 +127,7 @@ func benchmarkDecoder(b *testing.B, n int) { if len(buf0) > n-i { buf0 = buf0[:n-i] } - io.Copy(w, bytes.NewBuffer(buf0)) + w.Write(buf0) } w.Close() buf1 := compressed.Bytes() @@ -135,7 +135,7 @@ func benchmarkDecoder(b *testing.B, n int) { runtime.GC() b.StartTimer() for i := 0; i < b.N; i++ { - io.Copy(ioutil.Discard, NewReader(bytes.NewBuffer(buf1), LSB, 8)) + io.Copy(ioutil.Discard, NewReader(bytes.NewReader(buf1), LSB, 8)) } } diff --git a/libgo/go/compress/zlib/reader_test.go b/libgo/go/compress/zlib/reader_test.go index 3b02a08..218ccba1 100644 --- a/libgo/go/compress/zlib/reader_test.go +++ b/libgo/go/compress/zlib/reader_test.go @@ -102,7 +102,7 @@ var zlibTests = []zlibTest{ func TestDecompressor(t *testing.T) { b := new(bytes.Buffer) for _, tt := range zlibTests { - in := bytes.NewBuffer(tt.compressed) + in := bytes.NewReader(tt.compressed) zlib, err := NewReaderDict(in, tt.dict) if err != nil { if err != tt.err { |