Skip to content

time: ParseDuration can panic on invalid input #46883

Closed
@athomason

Description

@athomason

What version of Go are you using (go version)?

$ go1.17beta1 version
go version go1.17beta1 darwin/amd64
$ gotip version
go version devel go1.17-ff6f2051d9 Tue Jun 22 04:10:24 2021 +0000 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

darwin/amd64

What did you do?

While experimenting with the awesome new fuzzer, I encountered a class of panics in time.ParseDuration when it receives invalid input and calls into time.quote() to format an error message (playground):

package main

import (
	"fmt"
	"time"
)

func main() {
	if _, err := time.ParseDuration("\x85\x85"); err != nil {
		fmt.Println(err)
	}
}
$ gotip run parse_duration_panic.go
panic: runtime error: index out of range [2] with length 2

goroutine 1 [running]:
time.quote({0x10a209b, 0x2})
    /Users/adam/sdk/gotip/src/time/format.go:770 +0x45f
time.ParseDuration({0x10a209b, 0x2})
    /Users/adam/sdk/gotip/src/time/format.go:1500 +0x393
main.main()
    /Users/adam/parse_duration_panic.go:9 +0x25
exit status 2

time/format.go:770 is in time.quote, which was refactored in https://go-review.googlesource.com/c/go/+/267017, though I haven't bisected to be sure that's the cause.

What did you expect to see?

$ go1.16.5 run parse_duration_panic.go
time: invalid duration "��"

What did you see instead?

$ go1.17beta1 run parse_duration_panic.go
panic: runtime error: index out of range [2] with length 2

goroutine 1 [running]:
time.quote({0x10a209b, 0x2})
    /Users/adam/sdk/go1.17beta1/src/time/format.go:770 +0x45f
time.ParseDuration({0x10a209b, 0x2})
    /Users/adam/sdk/go1.17beta1/src/time/format.go:1500 +0x393
main.main()
    /Users/adam/parse_duration_panic.go:9 +0x25
exit status 2
$ gotip run parse_duration_panic.go
panic: runtime error: index out of range [2] with length 2

goroutine 1 [running]:
time.quote({0x10a209b, 0x2})
    /Users/adam/sdk/gotip/src/time/format.go:770 +0x45f
time.ParseDuration({0x10a209b, 0x2})
    /Users/adam/sdk/gotip/src/time/format.go:1500 +0x393
main.main()
    /Users/adam/parse_duration_panic.go:9 +0x25
exit status 2

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions