Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/gob: nil interface passed to userType #1626

Closed
rsc opened this issue Mar 19, 2011 · 1 comment
Closed

encoding/gob: nil interface passed to userType #1626

rsc opened this issue Mar 19, 2011 · 1 comment
Assignees

Comments

@rsc
Copy link
Contributor

@rsc rsc commented Mar 19, 2011

This line in type.go:

    tInterface = bootstrapType("interface", interface{}(nil), 8)

passes a nil to bootstrapType, which calls reflect.NewValue,
which returns a nil reflect.Type.  This manages to pass all the
way through validUserType without causing a crash, because the
type assertion comma-ok form simply fails if an interface is nil.
So the code ends up filling in userTypeCache[nil] with apparently
no ill effect.

Still it's probably not the intent.  One way to address this would
be to pass pointers to bootstrapType:


var (
    // Primordial types, needed during initialization.
    tBool      = bootstrapType("bool", new(bool), 1)
    tInt       = bootstrapType("int", new(int), 2)
    tUint      = bootstrapType("uint", new(uint), 3)
    tFloat     = bootstrapType("float", new(float64), 4)
    tBytes     = bootstrapType("bytes", new([]byte), 5)
    tString    = bootstrapType("string", new(string), 6)
    tComplex   = bootstrapType("complex", new(complex128), 7)
    tInterface = bootstrapType("interface", new(interface{}), 8)
    // Reserve some Ids for compatible expansion
    tReserved7 = bootstrapType("_reserved1", new(struct{ r7 int }), 9)
    tReserved6 = bootstrapType("_reserved1", new(struct{ r6 int }), 10)
    tReserved5 = bootstrapType("_reserved1", new(struct{ r5 int }), 11)
    tReserved4 = bootstrapType("_reserved1", new(struct{ r4 int }), 12)
    tReserved3 = bootstrapType("_reserved1", new(struct{ r3 int }), 13)
    tReserved2 = bootstrapType("_reserved1", new(struct{ r2 int }), 14)
    tReserved1 = bootstrapType("_reserved1", new(struct{ r1 int }), 15)
)

and then the first line of bootstrapType would be

    rt := reflect.Typeof(e).(*reflect.PtrType).Elem()

I tried this and it still passes the unit tests but I don't know
if there is some subtle effect that is not being caught.
@robpike
Copy link
Contributor

@robpike robpike commented Mar 22, 2011

Comment 1:

This issue was closed by revision 544cf75.

Status changed to Fixed.

@rsc rsc added fixed labels Mar 22, 2011
@mikioh mikioh changed the title gob: nil interface passed to userType encoding/gob: nil interface passed to userType Feb 26, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.