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

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

Closed
randall77 opened this issue Nov 12, 2018 · 2 comments
Closed
Milestone

Comments

@randall77
Copy link
Contributor

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
Copy link
Contributor Author

See also #6871.

@gopherbot
Copy link

Change https://golang.org/cl/175440 mentions this issue: reflect: special-case panic message when returning Value{} from MakeFunc function

@golang golang locked and limited conversation to collaborators May 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants