Skip to content

reflect: MethodByName is broken if unexported method sorts before exported method #21177

Closed
@ianlancetaylor

Description

@ianlancetaylor

I noticed while reading the code that reflect.(*rtype).MethodByName has been broken since Go 1.6 if an exported name happens to sort after an unexported method. This never happens when using pure ASCII, but can happen with non-ASCII uppercase letters.

Test case:

package main

import (
	"fmt"
	"reflect"
)

type I int

func (i I) ΦExported() {}
func (i I) unexported() {}

func main() {
	t := reflect.TypeOf(I(0))
	m, _ := t.MethodByName("ΦExported")
	fmt.Println(m)
}

When run with Go 1.6 this prints

{ΦExported  func(main.I) <func(main.I) Value> 1}

as it should. With Go 1.7 through 1.9rc1 it panics saying panic: reflect: Method index out of range.

The bug is that MethodByName walks through the methods by index, and then calls Method(index). But the index argument to Method is an index into the exported methods, not the index into all methods that MethodByName is using. It's the same value if all the exported methods happen to sort first, but of course is different if an exported method sorts after an unexported method.

Since this is not a regression, marking as 1.10.

CC @crawshaw

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions