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

reflect: MakeFunc can't handle return values which are the zero Value #28748

Open
randall77 opened this Issue Nov 12, 2018 · 1 comment

Comments

Projects
None yet
1 participant
@randall77
Contributor

randall77 commented Nov 12, 2018

package main

import "reflect"

func main() {
	r := reflect.MakeFunc(reflect.TypeOf(func() error { return nil }),
		func(args []reflect.Value) []reflect.Value {
			var x [1]reflect.Value
			return x[:]
		}).Interface().(func() error)
	r()
}

This code will cause the r() call to panic with a nil pointer dereference. We should really panic with something sensible instead.

The code is trying to provide a sensible panic, but panics when trying to build that panic message. It gets to here:

			if v.typ != typ {
				panic("reflect: function created by MakeFunc using " + funcName(f) +
					" returned wrong type: have " +
					out[i].typ.String() + " for " + typ.String())
			}

But v.typ is nil, so out[i].typ.String() (v and out[i] are the same) panics inside (*rtype).String.

This has been the case since at least go 1.2. Marking for 1.13.

@randall77 randall77 added this to the Go1.13 milestone Nov 12, 2018

@randall77

This comment has been minimized.

Contributor

randall77 commented Nov 13, 2018

See also #6871.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment