diff options
author | 6543 <6543@obermui.de> | 2021-06-10 16:44:25 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-10 16:44:25 +0200 |
commit | 86e2789960439ca786c6e7a74f85f076c223d148 (patch) | |
tree | c63064d4258bcf2bb7a2e5e60acc0a5d5c8216da /vendor/github.com/prometheus/common/model/time.go | |
parent | f088dc4ea14ae6304c2e06fca06ffa2f7bc273f3 (diff) | |
download | gitea-86e2789960439ca786c6e7a74f85f076c223d148.tar.gz gitea-86e2789960439ca786c6e7a74f85f076c223d148.zip |
Vendor Update (#16121)
* update github.com/PuerkitoBio/goquery
* update github.com/alecthomas/chroma
* update github.com/blevesearch/bleve/v2
* update github.com/caddyserver/certmagic
* update github.com/go-enry/go-enry/v2
* update github.com/go-git/go-billy/v5
* update github.com/go-git/go-git/v5
* update github.com/go-redis/redis/v8
* update github.com/go-testfixtures/testfixtures/v3
* update github.com/jaytaylor/html2text
* update github.com/json-iterator/go
* update github.com/klauspost/compress
* update github.com/markbates/goth
* update github.com/mattn/go-isatty
* update github.com/mholt/archiver/v3
* update github.com/microcosm-cc/bluemonday
* update github.com/minio/minio-go/v7
* update github.com/prometheus/client_golang
* update github.com/unrolled/render
* update github.com/xanzy/go-gitlab
* update github.com/yuin/goldmark
* update github.com/yuin/goldmark-highlighting
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Diffstat (limited to 'vendor/github.com/prometheus/common/model/time.go')
-rw-r--r-- | vendor/github.com/prometheus/common/model/time.go | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/vendor/github.com/prometheus/common/model/time.go b/vendor/github.com/prometheus/common/model/time.go index 77b82b2baa..7f67b16e42 100644 --- a/vendor/github.com/prometheus/common/model/time.go +++ b/vendor/github.com/prometheus/common/model/time.go @@ -14,6 +14,8 @@ package model import ( + "encoding/json" + "errors" "fmt" "math" "regexp" @@ -201,13 +203,23 @@ func ParseDuration(durationStr string) (Duration, error) { // Parse the match at pos `pos` in the regex and use `mult` to turn that // into ms, then add that value to the total parsed duration. + var overflowErr error m := func(pos int, mult time.Duration) { if matches[pos] == "" { return } n, _ := strconv.Atoi(matches[pos]) + + // Check if the provided duration overflows time.Duration (> ~ 290years). + if n > int((1<<63-1)/mult/time.Millisecond) { + overflowErr = errors.New("duration out of range") + } d := time.Duration(n) * time.Millisecond dur += d * mult + + if dur < 0 { + overflowErr = errors.New("duration out of range") + } } m(2, 1000*60*60*24*365) // y @@ -218,7 +230,7 @@ func ParseDuration(durationStr string) (Duration, error) { m(12, 1000) // s m(14, 1) // ms - return Duration(dur), nil + return Duration(dur), overflowErr } func (d Duration) String() string { @@ -254,6 +266,25 @@ func (d Duration) String() string { return r } +// MarshalJSON implements the json.Marshaler interface. +func (d Duration) MarshalJSON() ([]byte, error) { + return json.Marshal(d.String()) +} + +// UnmarshalJSON implements the json.Unmarshaler interface. +func (d *Duration) UnmarshalJSON(bytes []byte) error { + var s string + if err := json.Unmarshal(bytes, &s); err != nil { + return err + } + dur, err := ParseDuration(s) + if err != nil { + return err + } + *d = dur + return nil +} + // MarshalText implements the encoding.TextMarshaler interface. func (d *Duration) MarshalText() ([]byte, error) { return []byte(d.String()), nil |