gob: protect against pure recursive types.

There are further changes required for things like
recursive map types.  Recursive struct types work
but the mechanism needs generalization.  The
case handled in this CL is pathological since it
cannot be represented at all by gob, so it should
be handled separately. (Prior to this CL, encode
would recur forever.)

R=rsc
CC=golang-dev
https://golang.org/cl/4206041
This commit is contained in:
Rob Pike 2011-02-23 09:49:35 -08:00
parent da8e6eec9a
commit c9b90c9d70
4 changed files with 69 additions and 36 deletions

View file

@ -973,17 +973,31 @@ func TestIgnoredFields(t *testing.T) {
}
}
type Bad0 struct {
ch chan int
c float64
func TestBadRecursiveType(t *testing.T) {
type Rec ***Rec
var rec Rec
b := new(bytes.Buffer)
err := NewEncoder(b).Encode(&rec)
if err == nil {
t.Error("expected error; got none")
} else if strings.Index(err.String(), "recursive") < 0 {
t.Error("expected recursive type error; got", err)
}
// Can't test decode easily because we can't encode one, so we can't pass one to a Decoder.
}
type Bad0 struct {
CH chan int
C float64
}
var nilEncoder *Encoder
func TestInvalidField(t *testing.T) {
var bad0 Bad0
bad0.ch = make(chan int)
bad0.CH = make(chan int)
b := new(bytes.Buffer)
var nilEncoder *Encoder
err := nilEncoder.encode(b, reflect.NewValue(&bad0), userType(reflect.Typeof(&bad0)))
if err == nil {
t.Error("expected error; got none")