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/encoding | |
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/encoding')
-rw-r--r-- | libgo/go/encoding/ascii85/ascii85_test.go | 5 | ||||
-rw-r--r-- | libgo/go/encoding/asn1/asn1.go | 31 | ||||
-rw-r--r-- | libgo/go/encoding/asn1/asn1_test.go | 9 | ||||
-rw-r--r-- | libgo/go/encoding/asn1/marshal.go | 8 | ||||
-rw-r--r-- | libgo/go/encoding/base32/base32_test.go | 8 | ||||
-rw-r--r-- | libgo/go/encoding/base64/base64_test.go | 8 | ||||
-rw-r--r-- | libgo/go/encoding/binary/binary_test.go | 4 | ||||
-rw-r--r-- | libgo/go/encoding/binary/varint_test.go | 8 | ||||
-rw-r--r-- | libgo/go/encoding/gob/codec_test.go | 56 | ||||
-rw-r--r-- | libgo/go/encoding/gob/decode.go | 6 | ||||
-rw-r--r-- | libgo/go/encoding/gob/encoder_test.go | 4 | ||||
-rw-r--r-- | libgo/go/encoding/json/encode.go | 3 | ||||
-rw-r--r-- | libgo/go/encoding/json/encode_test.go | 2 | ||||
-rw-r--r-- | libgo/go/encoding/json/scanner_test.go | 22 | ||||
-rw-r--r-- | libgo/go/encoding/xml/read.go | 2 |
15 files changed, 134 insertions, 42 deletions
diff --git a/libgo/go/encoding/ascii85/ascii85_test.go b/libgo/go/encoding/ascii85/ascii85_test.go index dc1134d..77bc465 100644 --- a/libgo/go/encoding/ascii85/ascii85_test.go +++ b/libgo/go/encoding/ascii85/ascii85_test.go @@ -8,6 +8,7 @@ import ( "bytes" "io" "io/ioutil" + "strings" "testing" ) @@ -115,7 +116,7 @@ func TestDecode(t *testing.T) { func TestDecoder(t *testing.T) { for _, p := range pairs { - decoder := NewDecoder(bytes.NewBufferString(p.encoded)) + decoder := NewDecoder(strings.NewReader(p.encoded)) dbuf, err := ioutil.ReadAll(decoder) if err != nil { t.Fatal("Read failed", err) @@ -130,7 +131,7 @@ func TestDecoder(t *testing.T) { func TestDecoderBuffering(t *testing.T) { for bs := 1; bs <= 12; bs++ { - decoder := NewDecoder(bytes.NewBufferString(bigtest.encoded)) + decoder := NewDecoder(strings.NewReader(bigtest.encoded)) buf := make([]byte, len(bigtest.decoded)+12) var total int for total = 0; total < len(bigtest.decoded); { diff --git a/libgo/go/encoding/asn1/asn1.go b/libgo/go/encoding/asn1/asn1.go index dfcbf92..7a3c379 100644 --- a/libgo/go/encoding/asn1/asn1.go +++ b/libgo/go/encoding/asn1/asn1.go @@ -23,6 +23,7 @@ import ( "fmt" "math/big" "reflect" + "strconv" "time" ) @@ -197,6 +198,19 @@ func (oi ObjectIdentifier) Equal(other ObjectIdentifier) bool { return true } +func (oi ObjectIdentifier) String() string { + var s string + + for i, v := range oi { + if i > 0 { + s += "." + } + s += strconv.Itoa(v) + } + + return s +} + // parseObjectIdentifier parses an OBJECT IDENTIFIER from the given bytes and // returns it. An object identifier is a sequence of variable length integers // that are assigned in a hierarchy. @@ -634,6 +648,10 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam universalTag = tagGeneralizedTime } + if params.set { + universalTag = tagSet + } + expectedClass := classUniversal expectedTag := universalTag @@ -854,13 +872,20 @@ func setDefaultValue(v reflect.Value, params fieldParameters) (ok bool) { // // The following tags on struct fields have special meaning to Unmarshal: // -// optional marks the field as ASN.1 OPTIONAL -// [explicit] tag:x specifies the ASN.1 tag number; implies ASN.1 CONTEXT SPECIFIC -// default:x sets the default value for optional integer fields +// application specifies that a APPLICATION tag is used +// default:x sets the default value for optional integer fields +// explicit specifies that an additional, explicit tag wraps the implicit one +// optional marks the field as ASN.1 OPTIONAL +// set causes a SET, rather than a SEQUENCE type to be expected +// tag:x specifies the ASN.1 tag number; implies ASN.1 CONTEXT SPECIFIC // // If the type of the first field of a structure is RawContent then the raw // ASN1 contents of the struct will be stored in it. // +// If the type name of a slice element ends with "SET" then it's treated as if +// the "set" tag was set on it. This can be used with nested slices where a +// struct tag cannot be given. +// // Other ASN.1 types are not supported; if it encounters them, // Unmarshal returns a parse error. func Unmarshal(b []byte, val interface{}) (rest []byte, err error) { diff --git a/libgo/go/encoding/asn1/asn1_test.go b/libgo/go/encoding/asn1/asn1_test.go index ea98e02..b553f78 100644 --- a/libgo/go/encoding/asn1/asn1_test.go +++ b/libgo/go/encoding/asn1/asn1_test.go @@ -232,6 +232,10 @@ func TestObjectIdentifier(t *testing.T) { } } } + + if s := ObjectIdentifier([]int{1, 2, 3, 4}).String(); s != "1.2.3.4" { + t.Errorf("bad ObjectIdentifier.String(). Got %s, want 1.2.3.4", s) + } } type timeTest struct { @@ -397,6 +401,10 @@ type TestBigInt struct { X *big.Int } +type TestSet struct { + Ints []int `asn1:"set"` +} + var unmarshalTestData = []struct { in []byte out interface{} @@ -416,6 +424,7 @@ var unmarshalTestData = []struct { {[]byte{0x01, 0x01, 0xff}, newBool(true)}, {[]byte{0x30, 0x0b, 0x13, 0x03, 0x66, 0x6f, 0x6f, 0x02, 0x01, 0x22, 0x02, 0x01, 0x33}, &TestElementsAfterString{"foo", 0x22, 0x33}}, {[]byte{0x30, 0x05, 0x02, 0x03, 0x12, 0x34, 0x56}, &TestBigInt{big.NewInt(0x123456)}}, + {[]byte{0x30, 0x0b, 0x31, 0x09, 0x02, 0x01, 0x01, 0x02, 0x01, 0x02, 0x02, 0x01, 0x03}, &TestSet{Ints: []int{1, 2, 3}}}, } func TestUnmarshal(t *testing.T) { diff --git a/libgo/go/encoding/asn1/marshal.go b/libgo/go/encoding/asn1/marshal.go index ed17e41..da50cf2 100644 --- a/libgo/go/encoding/asn1/marshal.go +++ b/libgo/go/encoding/asn1/marshal.go @@ -568,6 +568,14 @@ func marshalField(out *forkableWriter, v reflect.Value, params fieldParameters) } // Marshal returns the ASN.1 encoding of val. +// +// In addition to the struct tags recognised by Unmarshal, the following can be +// used: +// +// ia5: causes strings to be marshaled as ASN.1, IA5 strings +// omitempty: causes empty slices to be skipped +// printable: causes strings to be marshaled as ASN.1, PrintableString strings. +// utf8: causes strings to be marshaled as ASN.1, UTF8 strings func Marshal(val interface{}) ([]byte, error) { var out bytes.Buffer v := reflect.ValueOf(val) diff --git a/libgo/go/encoding/base32/base32_test.go b/libgo/go/encoding/base32/base32_test.go index 63298d1..f56b996 100644 --- a/libgo/go/encoding/base32/base32_test.go +++ b/libgo/go/encoding/base32/base32_test.go @@ -108,7 +108,7 @@ func TestDecode(t *testing.T) { func TestDecoder(t *testing.T) { for _, p := range pairs { - decoder := NewDecoder(StdEncoding, bytes.NewBufferString(p.encoded)) + decoder := NewDecoder(StdEncoding, strings.NewReader(p.encoded)) dbuf := make([]byte, StdEncoding.DecodedLen(len(p.encoded))) count, err := decoder.Read(dbuf) if err != nil && err != io.EOF { @@ -125,7 +125,7 @@ func TestDecoder(t *testing.T) { func TestDecoderBuffering(t *testing.T) { for bs := 1; bs <= 12; bs++ { - decoder := NewDecoder(StdEncoding, bytes.NewBufferString(bigtest.encoded)) + decoder := NewDecoder(StdEncoding, strings.NewReader(bigtest.encoded)) buf := make([]byte, len(bigtest.decoded)+12) var total int for total = 0; total < len(bigtest.decoded); { @@ -267,13 +267,13 @@ LNEBUWIIDFON2CA3DBMJXXE5LNFY== ====` encodedShort := strings.Replace(encoded, "\n", "", -1) - dec := NewDecoder(StdEncoding, bytes.NewBufferString(encoded)) + dec := NewDecoder(StdEncoding, strings.NewReader(encoded)) res1, err := ioutil.ReadAll(dec) if err != nil { t.Errorf("ReadAll failed: %v", err) } - dec = NewDecoder(StdEncoding, bytes.NewBufferString(encodedShort)) + dec = NewDecoder(StdEncoding, strings.NewReader(encodedShort)) var res2 []byte res2, err = ioutil.ReadAll(dec) if err != nil { diff --git a/libgo/go/encoding/base64/base64_test.go b/libgo/go/encoding/base64/base64_test.go index 579591a..6bcc724 100644 --- a/libgo/go/encoding/base64/base64_test.go +++ b/libgo/go/encoding/base64/base64_test.go @@ -113,7 +113,7 @@ func TestDecode(t *testing.T) { func TestDecoder(t *testing.T) { for _, p := range pairs { - decoder := NewDecoder(StdEncoding, bytes.NewBufferString(p.encoded)) + decoder := NewDecoder(StdEncoding, strings.NewReader(p.encoded)) dbuf := make([]byte, StdEncoding.DecodedLen(len(p.encoded))) count, err := decoder.Read(dbuf) if err != nil && err != io.EOF { @@ -130,7 +130,7 @@ func TestDecoder(t *testing.T) { func TestDecoderBuffering(t *testing.T) { for bs := 1; bs <= 12; bs++ { - decoder := NewDecoder(StdEncoding, bytes.NewBufferString(bigtest.encoded)) + decoder := NewDecoder(StdEncoding, strings.NewReader(bigtest.encoded)) buf := make([]byte, len(bigtest.decoded)+12) var total int for total = 0; total < len(bigtest.decoded); { @@ -308,13 +308,13 @@ bqbPb06551Y4 ` encodedShort := strings.Replace(encoded, "\n", "", -1) - dec := NewDecoder(StdEncoding, bytes.NewBufferString(encoded)) + dec := NewDecoder(StdEncoding, strings.NewReader(encoded)) res1, err := ioutil.ReadAll(dec) if err != nil { t.Errorf("ReadAll failed: %v", err) } - dec = NewDecoder(StdEncoding, bytes.NewBufferString(encodedShort)) + dec = NewDecoder(StdEncoding, strings.NewReader(encodedShort)) var res2 []byte res2, err = ioutil.ReadAll(dec) if err != nil { diff --git a/libgo/go/encoding/binary/binary_test.go b/libgo/go/encoding/binary/binary_test.go index fdfee7d..1aa6ecd 100644 --- a/libgo/go/encoding/binary/binary_test.go +++ b/libgo/go/encoding/binary/binary_test.go @@ -111,7 +111,7 @@ func checkResult(t *testing.T, dir string, order ByteOrder, err error, have, wan func testRead(t *testing.T, order ByteOrder, b []byte, s1 interface{}) { var s2 Struct - err := Read(bytes.NewBuffer(b), order, &s2) + err := Read(bytes.NewReader(b), order, &s2) checkResult(t, "Read", order, err, s2, s1) } @@ -131,7 +131,7 @@ func TestBigEndianPtrWrite(t *testing.T) { testWrite(t, BigEndian, big, &s) } func TestReadSlice(t *testing.T) { slice := make([]int32, 2) - err := Read(bytes.NewBuffer(src), BigEndian, slice) + err := Read(bytes.NewReader(src), BigEndian, slice) checkResult(t, "ReadSlice", BigEndian, err, slice, res) } diff --git a/libgo/go/encoding/binary/varint_test.go b/libgo/go/encoding/binary/varint_test.go index 9476bd5..ca411ec 100644 --- a/libgo/go/encoding/binary/varint_test.go +++ b/libgo/go/encoding/binary/varint_test.go @@ -35,7 +35,7 @@ func testVarint(t *testing.T, x int64) { t.Errorf("Varint(%d): got n = %d; want %d", x, m, n) } - y, err := ReadVarint(bytes.NewBuffer(buf)) + y, err := ReadVarint(bytes.NewReader(buf)) if err != nil { t.Errorf("ReadVarint(%d): %s", x, err) } @@ -55,7 +55,7 @@ func testUvarint(t *testing.T, x uint64) { t.Errorf("Uvarint(%d): got n = %d; want %d", x, m, n) } - y, err := ReadUvarint(bytes.NewBuffer(buf)) + y, err := ReadUvarint(bytes.NewReader(buf)) if err != nil { t.Errorf("ReadUvarint(%d): %s", x, err) } @@ -114,7 +114,7 @@ func TestBufferTooSmall(t *testing.T) { t.Errorf("Uvarint(%v): got x = %d, n = %d", buf, x, n) } - x, err := ReadUvarint(bytes.NewBuffer(buf)) + x, err := ReadUvarint(bytes.NewReader(buf)) if x != 0 || err != io.EOF { t.Errorf("ReadUvarint(%v): got x = %d, err = %s", buf, x, err) } @@ -127,7 +127,7 @@ func testOverflow(t *testing.T, buf []byte, n0 int, err0 error) { t.Errorf("Uvarint(%v): got x = %d, n = %d; want 0, %d", buf, x, n, n0) } - x, err := ReadUvarint(bytes.NewBuffer(buf)) + x, err := ReadUvarint(bytes.NewReader(buf)) if x != 0 || err != err0 { t.Errorf("ReadUvarint(%v): got x = %d, err = %s; want 0, %s", buf, x, err, err0) } diff --git a/libgo/go/encoding/gob/codec_test.go b/libgo/go/encoding/gob/codec_test.go index b40f783..fa57f37 100644 --- a/libgo/go/encoding/gob/codec_test.go +++ b/libgo/go/encoding/gob/codec_test.go @@ -1364,11 +1364,7 @@ type DT struct { S []string } -func TestDebugStruct(t *testing.T) { - if debugFunc == nil { - return - } - Register(OnTheFly{}) +func newDT() DT { var dt DT dt.A = 17 dt.B = "hello" @@ -1379,6 +1375,15 @@ func TestDebugStruct(t *testing.T) { dt.M = map[string]int{"one": 1, "two": 2} dt.T = [3]int{11, 22, 33} dt.S = []string{"hi", "joe"} + return dt +} + +func TestDebugStruct(t *testing.T) { + if debugFunc == nil { + return + } + Register(OnTheFly{}) + dt := newDT() b := new(bytes.Buffer) err := NewEncoder(b).Encode(dt) if err != nil { @@ -1458,3 +1463,44 @@ func testFuzz(t *testing.T, seed int64, n int, input ...interface{}) { } } } + +// TestFuzzOneByte tries to decode corrupted input sequences +// and checks that no panic occurs. +func TestFuzzOneByte(t *testing.T) { + buf := new(bytes.Buffer) + Register(OnTheFly{}) + dt := newDT() + if err := NewEncoder(buf).Encode(dt); err != nil { + t.Fatal(err) + } + s := buf.String() + + indices := make([]int, 0, len(s)) + for i := 0; i < len(s); i++ { + switch i { + case 14, 167, 231, 265: // a slice length, corruptions are not handled yet. + continue + } + indices = append(indices, i) + } + if testing.Short() { + indices = []int{1, 111, 178} // known fixed panics + } + for _, i := range indices { + for j := 0; j < 256; j += 3 { + b := []byte(s) + b[i] ^= byte(j) + var e DT + func() { + defer func() { + if p := recover(); p != nil { + t.Errorf("crash for b[%d] ^= 0x%x", i, j) + panic(p) + } + }() + err := NewDecoder(bytes.NewReader(b)).Decode(&e) + _ = err + }() + } + } +} diff --git a/libgo/go/encoding/gob/decode.go b/libgo/go/encoding/gob/decode.go index 3037a58..aa186a5 100644 --- a/libgo/go/encoding/gob/decode.go +++ b/libgo/go/encoding/gob/decode.go @@ -701,6 +701,9 @@ func (dec *Decoder) decodeInterface(ityp reflect.Type, state *decoderState, p un if nr < 0 || nr > 1<<31 { // zero is permissible for anonymous types errorf("invalid type name length %d", nr) } + if nr > uint64(state.b.Len()) { + errorf("invalid type name length %d: exceeds input size", nr) + } b := make([]byte, nr) state.b.Read(b) name := string(b) @@ -1237,7 +1240,8 @@ func (dec *Decoder) decodeValue(wireId typeId, val reflect.Value) { } engine := *enginePtr if st := base; st.Kind() == reflect.Struct && ut.externalDec == 0 { - if engine.numInstr == 0 && st.NumField() > 0 && len(dec.wireType[wireId].StructT.Field) > 0 { + if engine.numInstr == 0 && st.NumField() > 0 && + dec.wireType[wireId] != nil && len(dec.wireType[wireId].StructT.Field) > 0 { name := base.Name() errorf("type mismatch: no fields matched compiling decoder for %s", name) } diff --git a/libgo/go/encoding/gob/encoder_test.go b/libgo/go/encoding/gob/encoder_test.go index 4ecf51d..6445ce1 100644 --- a/libgo/go/encoding/gob/encoder_test.go +++ b/libgo/go/encoding/gob/encoder_test.go @@ -129,6 +129,8 @@ func TestBadData(t *testing.T) { corruptDataCheck("", io.EOF, t) corruptDataCheck("\x7Fhi", io.ErrUnexpectedEOF, t) corruptDataCheck("\x03now is the time for all good men", errBadType, t) + // issue 6323. + corruptDataCheck("\x04\x24foo", errRange, t) } // Types not supported at top level by the Encoder. @@ -630,7 +632,7 @@ func TestSliceReusesMemory(t *testing.T) { // Used to crash: negative count in recvMessage. func TestBadCount(t *testing.T) { b := []byte{0xfb, 0xa5, 0x82, 0x2f, 0xca, 0x1} - if err := NewDecoder(bytes.NewBuffer(b)).Decode(nil); err == nil { + if err := NewDecoder(bytes.NewReader(b)).Decode(nil); err == nil { t.Error("expected error from bad count") } else if err.Error() != errBadCount.Error() { t.Error("expected bad count error; got", err) diff --git a/libgo/go/encoding/json/encode.go b/libgo/go/encoding/json/encode.go index 4a77ba1..741ddd8 100644 --- a/libgo/go/encoding/json/encode.go +++ b/libgo/go/encoding/json/encode.go @@ -44,6 +44,7 @@ import ( // if an invalid UTF-8 sequence is encountered. // The angle brackets "<" and ">" are escaped to "\u003c" and "\u003e" // to keep some browsers from misinterpreting JSON output as HTML. +// Ampersand "&" is also escaped to "\u0026" for the same reason. // // Array and slice values encode as JSON arrays, except that // []byte encodes as a base64-encoded string, and a nil slice @@ -804,7 +805,7 @@ func (e *encodeState) string(s string) (int, error) { e.WriteByte('r') default: // This encodes bytes < 0x20 except for \n and \r, - // as well as < and >. The latter are escaped because they + // as well as <, > and &. The latter are escaped because they // can lead to security holes when user-controlled strings // are rendered into JSON and served to some browsers. e.WriteString(`\u00`) diff --git a/libgo/go/encoding/json/encode_test.go b/libgo/go/encoding/json/encode_test.go index 265a237..2e89a78 100644 --- a/libgo/go/encoding/json/encode_test.go +++ b/libgo/go/encoding/json/encode_test.go @@ -93,7 +93,7 @@ func TestStringTag(t *testing.T) { // Verify that it round-trips. var s2 StringTag - err = NewDecoder(bytes.NewBuffer(got)).Decode(&s2) + err = NewDecoder(bytes.NewReader(got)).Decode(&s2) if err != nil { t.Fatalf("Decode: %v", err) } diff --git a/libgo/go/encoding/json/scanner_test.go b/libgo/go/encoding/json/scanner_test.go index 90e45ff..7880342 100644 --- a/libgo/go/encoding/json/scanner_test.go +++ b/libgo/go/encoding/json/scanner_test.go @@ -239,23 +239,16 @@ func trim(b []byte) []byte { var jsonBig []byte -const ( - big = 10000 - small = 100 -) - func initBig() { - n := big + n := 10000 if testing.Short() { - n = small + n = 100 } - if len(jsonBig) != n { - b, err := Marshal(genValue(n)) - if err != nil { - panic(err) - } - jsonBig = b + b, err := Marshal(genValue(n)) + if err != nil { + panic(err) } + jsonBig = b } func genValue(n int) interface{} { @@ -296,6 +289,9 @@ func genArray(n int) []interface{} { if f > n { f = n } + if f < 1 { + f = 1 + } x := make([]interface{}, f) for i := range x { x[i] = genValue(((i+1)*n)/f - (i*n)/f) diff --git a/libgo/go/encoding/xml/read.go b/libgo/go/encoding/xml/read.go index 8890508..651d13d4 100644 --- a/libgo/go/encoding/xml/read.go +++ b/libgo/go/encoding/xml/read.go @@ -112,7 +112,7 @@ import ( // to a freshly allocated value and then mapping the element to that value. // func Unmarshal(data []byte, v interface{}) error { - return NewDecoder(bytes.NewBuffer(data)).Decode(v) + return NewDecoder(bytes.NewReader(data)).Decode(v) } // Decode works like xml.Unmarshal, except it reads the decoder |