From 593f74bbab63d34c7060918088bcbad686c31c66 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Tue, 6 Mar 2012 17:57:23 +0000 Subject: libgo: Update to weekly.2012-03-04 release. From-SVN: r185010 --- libgo/go/encoding/binary/binary.go | 8 ++------ libgo/go/encoding/gob/codec_test.go | 5 ++++- libgo/go/encoding/gob/debug.go | 5 +++-- libgo/go/encoding/gob/decode.go | 23 +++++++++++++++++------ libgo/go/encoding/gob/dump.go | 1 + libgo/go/encoding/gob/encoder_test.go | 2 +- libgo/go/encoding/json/decode_test.go | 10 ---------- libgo/go/encoding/json/encode.go | 18 +----------------- libgo/go/encoding/json/encode_test.go | 19 +++++++++++++++++++ libgo/go/encoding/json/indent.go | 13 +++++++++++++ libgo/go/encoding/xml/marshal_test.go | 16 ++++++++-------- 11 files changed, 69 insertions(+), 51 deletions(-) (limited to 'libgo/go/encoding') diff --git a/libgo/go/encoding/binary/binary.go b/libgo/go/encoding/binary/binary.go index b26b1bb..02f090d 100644 --- a/libgo/go/encoding/binary/binary.go +++ b/libgo/go/encoding/binary/binary.go @@ -29,17 +29,13 @@ type ByteOrder interface { String() string } -// This is byte instead of struct{} so that it can be compared, -// allowing, e.g., order == binary.LittleEndian. -type unused byte - // LittleEndian is the little-endian implementation of ByteOrder. var LittleEndian littleEndian // BigEndian is the big-endian implementation of ByteOrder. var BigEndian bigEndian -type littleEndian unused +type littleEndian struct{} func (littleEndian) Uint16(b []byte) uint16 { return uint16(b[0]) | uint16(b[1])<<8 } @@ -79,7 +75,7 @@ func (littleEndian) String() string { return "LittleEndian" } func (littleEndian) GoString() string { return "binary.LittleEndian" } -type bigEndian unused +type bigEndian struct{} func (bigEndian) Uint16(b []byte) uint16 { return uint16(b[1]) | uint16(b[0])<<8 } diff --git a/libgo/go/encoding/gob/codec_test.go b/libgo/go/encoding/gob/codec_test.go index d365f82..ebcbb78 100644 --- a/libgo/go/encoding/gob/codec_test.go +++ b/libgo/go/encoding/gob/codec_test.go @@ -1455,11 +1455,14 @@ func TestFuzz(t *testing.T) { func TestFuzzRegressions(t *testing.T) { // An instance triggering a type name of length ~102 GB. testFuzz(t, 1328492090837718000, 100, new(float32)) + // An instance triggering a type name of 1.6 GB. + // Commented out because it takes 5m to run. + //testFuzz(t, 1330522872628565000, 100, new(int)) } func testFuzz(t *testing.T, seed int64, n int, input ...interface{}) { - t.Logf("seed=%d n=%d\n", seed, n) for _, e := range input { + t.Logf("seed=%d n=%d e=%T", seed, n, e) rng := rand.New(rand.NewSource(seed)) for i := 0; i < n; i++ { encFuzzDec(rng, e) diff --git a/libgo/go/encoding/gob/debug.go b/libgo/go/encoding/gob/debug.go index b54ef46..31d1351 100644 --- a/libgo/go/encoding/gob/debug.go +++ b/libgo/go/encoding/gob/debug.go @@ -3,14 +3,15 @@ // license that can be found in the LICENSE file. // Delete the next line to include in the gob package. -// +build gob-debug +// +build ignore package gob // This file is not normally included in the gob package. Used only for debugging the package itself. -// Add debug.go to the files listed in the Makefile to add Debug to the gob package. // Except for reading uints, it is an implementation of a reader that is independent of // the one implemented by Decoder. +// To enable the Debug function, delete the +build ignore line above and do +// go install import ( "bytes" diff --git a/libgo/go/encoding/gob/decode.go b/libgo/go/encoding/gob/decode.go index a0bb985..0708a83 100644 --- a/libgo/go/encoding/gob/decode.go +++ b/libgo/go/encoding/gob/decode.go @@ -392,12 +392,12 @@ func decUint8Slice(i *decInstr, state *decoderState, p unsafe.Pointer) { } p = *(*unsafe.Pointer)(p) } - n := int(state.decodeUint()) - if n < 0 { - errorf("negative length decoding []byte") + n := state.decodeUint() + if n > uint64(state.b.Len()) { + errorf("length of []byte exceeds input size (%d bytes)", n) } slice := (*[]uint8)(p) - if cap(*slice) < n { + if uint64(cap(*slice)) < n { *slice = make([]uint8, n) } else { *slice = (*slice)[0:n] @@ -417,7 +417,11 @@ func decString(i *decInstr, state *decoderState, p unsafe.Pointer) { } p = *(*unsafe.Pointer)(p) } - b := make([]byte, state.decodeUint()) + n := state.decodeUint() + if n > uint64(state.b.Len()) { + errorf("string length exceeds input size (%d bytes)", n) + } + b := make([]byte, n) state.b.Read(b) // It would be a shame to do the obvious thing here, // *(*string)(p) = string(b) @@ -647,7 +651,11 @@ func (dec *Decoder) ignoreMap(state *decoderState, keyOp, elemOp decOp) { // decodeSlice decodes a slice and stores the slice header through p. // Slices are encoded as an unsigned length followed by the elements. func (dec *Decoder) decodeSlice(atyp reflect.Type, state *decoderState, p uintptr, elemOp decOp, elemWid uintptr, indir, elemIndir int, ovfl error) { - n := int(uintptr(state.decodeUint())) + nr := state.decodeUint() + if nr > uint64(state.b.Len()) { + errorf("length of slice exceeds input size (%d elements)", nr) + } + n := int(nr) if indir > 0 { up := unsafe.Pointer(p) if *(*unsafe.Pointer)(up) == nil { @@ -702,6 +710,9 @@ func (dec *Decoder) decodeInterface(ityp reflect.Type, state *decoderState, p ui *(*[2]uintptr)(unsafe.Pointer(p)) = ivalue.InterfaceData() return } + if len(name) > 1024 { + errorf("name too long (%d bytes): %.20q...", len(name), name) + } // The concrete type must be registered. typ, ok := nameToConcreteType[name] if !ok { diff --git a/libgo/go/encoding/gob/dump.go b/libgo/go/encoding/gob/dump.go index e23a11e..17238c9 100644 --- a/libgo/go/encoding/gob/dump.go +++ b/libgo/go/encoding/gob/dump.go @@ -7,6 +7,7 @@ package main // Need to compile package gob with debug.go to build this program. +// See comments in debug.go for how to do this. import ( "encoding/gob" diff --git a/libgo/go/encoding/gob/encoder_test.go b/libgo/go/encoding/gob/encoder_test.go index 3bfae30..050786d 100644 --- a/libgo/go/encoding/gob/encoder_test.go +++ b/libgo/go/encoding/gob/encoder_test.go @@ -709,7 +709,7 @@ func TestGobPtrSlices(t *testing.T) { t.Fatal("decode:", err) } if !reflect.DeepEqual(in, out) { - t.Fatal("got %v; wanted %v", out, in) + t.Fatalf("got %v; wanted %v", out, in) } } diff --git a/libgo/go/encoding/json/decode_test.go b/libgo/go/encoding/json/decode_test.go index 0eec586..d758758 100644 --- a/libgo/go/encoding/json/decode_test.go +++ b/libgo/go/encoding/json/decode_test.go @@ -239,16 +239,6 @@ func TestEscape(t *testing.T) { } } -func TestHTMLEscape(t *testing.T) { - b, err := MarshalForHTML("foobarbaz<>&quux") - if err != nil { - t.Fatalf("MarshalForHTML error: %v", err) - } - if !bytes.Equal(b, []byte(`"foobarbaz\u003c\u003e\u0026quux"`)) { - t.Fatalf("Unexpected encoding of \"<>&\": %s", b) - } -} - // WrongString is a struct that's misusing the ,string modifier. type WrongString struct { Message string `json:"result,string"` diff --git a/libgo/go/encoding/json/encode.go b/libgo/go/encoding/json/encode.go index 8a794b7..5425a3a 100644 --- a/libgo/go/encoding/json/encode.go +++ b/libgo/go/encoding/json/encode.go @@ -123,17 +123,6 @@ func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) { return buf.Bytes(), nil } -// MarshalForHTML is like Marshal but applies HTMLEscape to the output. -func MarshalForHTML(v interface{}) ([]byte, error) { - b, err := Marshal(v) - if err != nil { - return nil, err - } - var buf bytes.Buffer - HTMLEscape(&buf, b) - return buf.Bytes(), nil -} - // HTMLEscape appends to dst the JSON-encoded src with <, >, and & // characters inside string literals changed to \u003c, \u003e, \u0026 // so that the JSON will be safe to embed inside HTML