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

x/tools/cmd/stringer: index out of range in generated code #10563

Closed
ncw opened this issue Apr 23, 2015 · 3 comments
Closed

x/tools/cmd/stringer: index out of range in generated code #10563

ncw opened this issue Apr 23, 2015 · 3 comments

Comments

@ncw
Copy link
Contributor

ncw commented Apr 23, 2015

Save this code to stringerbug/main.go

package main

import "fmt"

type Blob uint8

//go:generate stringer -type=Blob -output stringer.go
const (
    A Blob = iota
    B
    C
)

func main() {
    fmt.Printf("Blob(255) = %s", Blob(255).String())
}

Run

go generate
go run
./stringerbug

This produces the following panic

panic: runtime error: index out of range

goroutine 1 [running]:
main.Blob.String(0x54d3ff, 0x0, 0x0)
    /home/ncw/Go/stringerbug/stringer.go:15 +0x1b6
main.main()
    /home/ncw/Go/stringerbug/main.go:15 +0x2b

The offending code being that generated by stringer

func (i Blob) String() string {
    if i+1 >= Blob(len(_Blob_index)) {
        return fmt.Sprintf("Blob(%d)", i)
    }
    return _Blob_name[_Blob_index[i]:_Blob_index[i+1]]
}

Unfortunately adding 1 to i in the above has caused the integer to wrap. Rewriting to

func (i Blob) String() string {
    if i >= Blob(len(_Blob_index)-1) {
        return fmt.Sprintf("Blob(%d)", i)
    }
    return _Blob_name[_Blob_index[i]:_Blob_index[i+1]]
}

Should fix it.

@ncw
Copy link
Contributor Author

ncw commented Apr 23, 2015

Proposed fix here: https://go-review.googlesource.com/9255

@gopherbot
Copy link

CL https://golang.org/cl/9255 mentions this issue.

@enormouspenguin
Copy link

I can see that this issue had already been fixed. But yesterday, although I did a fresh go get golang.org/x/tools/cmd/stringer, I still experience this same issue. Why does it still persist? Does anyone care enough to apply the fix to the main repo?

@golang golang locked and limited conversation to collaborators Jun 25, 2016
SOF3 pushed a commit to SOF3/go-stringer-inverse that referenced this issue Aug 23, 2018
When String() was called on the maximum value of an integer type (eg
255 for uint8) this would cause an integer overflow, which would cause
an index error later in the code.

Fixed by re-arranging the code slightly.

Fixes golang/go#10563

Change-Id: I9fd016afc5eea22adbc3843f6081091fd50deccf
Reviewed-on: https://go-review.googlesource.com/9255
Reviewed-by: Rob Pike <r@golang.org>
SOF3 pushed a commit to SOF3/go-stringer-inverse that referenced this issue Aug 23, 2018
When String() was called on the maximum value of an integer type (eg
255 for uint8) this would cause an integer overflow, which would cause
an index error later in the code.

Fixed by re-arranging the code slightly.

Fixes golang/go#10563

Change-Id: I9fd016afc5eea22adbc3843f6081091fd50deccf
Reviewed-on: https://go-review.googlesource.com/9255
Reviewed-by: Rob Pike <r@golang.org>
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

3 participants