1
0
mirror of https://github.com/golang/go synced 2024-10-01 16:18:32 -06:00
go/cmd/stringer/testdata/unum2.go
Nick Craig-Wood 578c521fc2 cmd/stringer: fix panic caused by integer overflow
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>
2015-06-02 19:44:20 +00:00

32 lines
494 B
Go

// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Unsigned integers - check maximum size
package main
import "fmt"
type Unum2 uint8
const (
Zero Unum2 = iota
One
Two
)
func main() {
ck(Zero, "Zero")
ck(One, "One")
ck(Two, "Two")
ck(3, "Unum2(3)")
ck(255, "Unum2(255)")
}
func ck(unum Unum2, str string) {
if fmt.Sprint(unum) != str {
panic("unum.go: " + str)
}
}