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

go-yaml accepts invalid durations #200

Open
azylman opened this issue Sep 19, 2016 · 3 comments
Open

go-yaml accepts invalid durations #200

azylman opened this issue Sep 19, 2016 · 3 comments

Comments

@azylman
Copy link

azylman commented Sep 19, 2016

When you call ParseDuration with an invalid duration (e.g. 15) it errors: https://play.golang.org/p/WAq-o3NT3D

However, if you use go-yaml and try to marshal an invalid duration such as 15 into a time.Duration, it accepts it and treats it as a number of nanoseconds.

test.yaml

thing: 15

test.go

package main

import (
        "io/ioutil"
        "log"
        "time"

        yaml "gopkg.in/yaml.v2"
)

type Spec struct {
        Thing time.Duration `yaml:"thing"`
}

func main() {
        data, err := ioutil.ReadFile("./test.yaml")
        if err != nil {
            panic(err)
        }

        var s Spec
        if err := yaml.Unmarshal(data, &s); err != nil {
            panic(err)
        }

        log.Printf("%#v", s)
}

output

2016/09/19 13:48:07 main.Spec{Thing:15}
@niemeyer
Copy link
Contributor

This has changed as suggested in v3, so time.Duration won't accept decoding from ints.

Will close this once v3 is released.

niemeyer added a commit that referenced this issue Apr 3, 2019
It used to work in v2, but that's very error prone as it's easy to say,
for example, 10 meaning 10 seconds, but it's really 10 nanoseconds.

Closes #200.
@szyhf
Copy link

szyhf commented Jul 22, 2019

@niemeyer I think may be accept "0" as validate time.Duration and reject any other int is more convenience to use~

@gurza
Copy link

gurza commented Aug 16, 2021

Unmarshaling of time.Duration works in v3, https://play.golang.org/p/-6y0zq96gVz

colega added a commit to colega/go-yaml-yaml that referenced this issue Jul 20, 2022
While in go-yaml#200 it was correctly
stated that durations without units are misleading, there's an exception
to that: zero duration.

This is a special case in time.Duration.ParseDuration():
https://github.com/golang/go/blob/176b63e7113b82c140a4ecb2620024526c2c42e3/src/time/format.go#L1536-L1539

And supporting this improves backwards compatibility with YAMLs written
for v2, as its very common to write just a zero for zero duration
(especially because this works for other config-parsing sources like
flags).

Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com>
colega added a commit to colega/go-yaml-yaml that referenced this issue Jul 20, 2022
While in go-yaml#200 it was correctly
stated that durations without units are misleading, there's an exception
to that: zero duration.

This is a special case in time.Duration.ParseDuration():
https://github.com/golang/go/blob/176b63e7113b82c140a4ecb2620024526c2c42e3/src/time/format.go#L1536-L1539

And supporting this improves backwards compatibility with YAMLs written
for v2, as its very common to write just a zero for zero duration
(especially because this works for other config-parsing sources like
flags).

Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants