Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

go-yaml should handle invalid integers as strings #157

@mbruzek

Description

@mbruzek

Starting a numeric sequence with zero indicates an octal number. So 01234567 == 342391 (base 10). Numeric sequences that start with zero and contain numbers higher than 7 are invalid integers and should be parsed as strings even when they do not have quotes around them.

key: 01182252

01182252 is not a valid octal integer and should be treated as a string. go-yaml attempts to parse the numeric sequence as an integer and this is incorrect.

Several yaml validators and other language yaml packages handle this case correctly such as the Python ruamel package and on line parsers http://yaml-online-parser.appspot.com/

package main

import (
    "fmt"
    "gopkg.in/v2/yaml"
)

var a = make(map[string]interface{})

func main() {
    data := []byte(`key: 01234567`)
    err := yaml.Unmarshal(data, a)
    if err != nil {
        fmt.Printf("%v\n", err)
    }
    fmt.Printf("%v\n", a["key"])

    // This value is surrounded by quotes.
    data = []byte(`key: "01182252"`)
    err = yaml.Unmarshal(data, a)
    if err != nil {
        fmt.Printf("%v\n", err)
    }
    fmt.Printf("%v\n", a["key"])

    // This value is not a valid octal integer therefore a string.
    data = []byte(`key: 01182252`)
    err = yaml.Unmarshal(data, a)
    if err != nil {
        fmt.Printf("%v\n", err)
    }
    fmt.Printf("%v\n", a["key"])
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions