Commit 94ef1daf authored by fraenkel's avatar fraenkel Committed by Brad Fitzpatrick

time: don't panic when stringifying Weekday

Fixes #24692

Change-Id: I14058cd3968d08fbcfc275f1b13b6dba9e3c5068
Reviewed-on: https://go-review.googlesource.com/106535
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 2dfb423e
......@@ -323,7 +323,14 @@ var days = [...]string{
}
// String returns the English name of the day ("Sunday", "Monday", ...).
func (d Weekday) String() string { return days[d] }
func (d Weekday) String() string {
if Sunday <= d && d <= Saturday {
return days[d]
}
buf := make([]byte, 20)
n := fmtInt(buf, uint64(d))
return "%!Weekday(" + string(buf[n:]) + ")"
}
// Computations on time.
//
......
......@@ -673,7 +673,7 @@ var gobTests = []Time{
Date(0, 1, 2, 3, 4, 5, 6, UTC),
Date(7, 8, 9, 10, 11, 12, 13, FixedZone("", 0)),
Unix(81985467080890095, 0x76543210), // Time.sec: 0x0123456789ABCDEF
{}, // nil location
{}, // nil location
Date(1, 2, 3, 4, 5, 6, 7, FixedZone("", 32767*60)),
Date(1, 2, 3, 4, 5, 6, 7, FixedZone("", -32768*60)),
}
......@@ -1319,6 +1319,16 @@ func TestZeroMonthString(t *testing.T) {
}
}
// Issue 24692: Out of range weekday panics
func TestWeekdayString(t *testing.T) {
if got, want := Weekday(Tuesday).String(), "Tuesday"; got != want {
t.Errorf("Tuesday weekday = %q; want %q", got, want)
}
if got, want := Weekday(14).String(), "%!Weekday(14)"; got != want {
t.Errorf("14th weekday = %q; want %q", got, want)
}
}
func TestReadFileLimit(t *testing.T) {
const zero = "/dev/zero"
if _, err := os.Stat(zero); err != nil {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment