freeconf / yang

Standards-based management for Golang microservices
Apache License 2.0
38 stars 15 forks source link

Parsing from file fails with comment after module definition #118

Open preetjuniper opened 2 weeks ago

preetjuniper commented 2 weeks ago

Loading the following from file fails (not sure if this is invalid YANG, I didn't see anything in the spec, or get any complaints from pyang)

YANG file:

module x {
  namespace "";
  prefix "";
  revision 0;
} // foo

example:

package main

import (
    "github.com/freeconf/yang/parser"
    "github.com/freeconf/yang/source"
    "github.com/freeconf/yang/fc"
)

func main() {
    fc.DebugLog(true)

    // load model
    _, errLdMdl := parser.LoadModule(source.Dir("yang"), "foo")
    if errLdMdl != nil {
        panic(errLdMdl.Error())
    }
}

Output:

panic: runtime error: index out of range [63] with length 63

goroutine 1 [running]:
github.com/freeconf/yang/parser.(*lexer).acceptWS(0xc00010c5a0)
    /dev/go/pkg/mod/github.com/freeconf/yang@v0.0.0-20240722123345-493940563c44/parser/lexer.go:278 +0xcf
github.com/freeconf/yang/parser.(*lexer).emit(0x55a63b?, 0xc00010c5a0?)
    /dev/go/pkg/mod/github.com/freeconf/yang@v0.0.0-20240722123345-493940563c44/parser/lexer.go:766 +0xb8
github.com/freeconf/yang/parser.(*lexer).acceptToken(0xc00010c5a0, 0xe007)
    /dev/go/pkg/mod/github.com/freeconf/yang@v0.0.0-20240722123345-493940563c44/parser/lexer.go:328 +0x19a
github.com/freeconf/yang/parser.lexBegin(0xc00010c5a0)
    /dev/go/pkg/mod/github.com/freeconf/yang@v0.0.0-20240722123345-493940563c44/parser/lexer.go:724 +0x122e
github.com/freeconf/yang/parser.(*lexer).nextToken(0xc00010c5a0)
    /dev/go/pkg/mod/github.com/freeconf/yang@v0.0.0-20240722123345-493940563c44/parser/lexer.go:792 +0x38
github.com/freeconf/yang/parser.(*lexer).Lex(0xc000059ba0?, 0xc000132588)
    parser.y:24 +0x18
github.com/freeconf/yang/parser.yylex1({0x607fe0?, 0xc00010c5a0?}, 0x0?)
    yaccpar:119 +0x2b
github.com/freeconf/yang/parser.(*yyParserImpl).Parse(0xc000132588, {0x607fe0, 0xc00010c5a0})
    yaccpar:204 +0x7e33
github.com/freeconf/yang/parser.yyParse(...)
    yaccpar:153
github.com/freeconf/yang/parser.(*parser).parseModule(0x6079e0?, {0xc000020140?, 0x3f?}, 0x0, {0x608008, 0xc0000308a0}, 0x796d20?)
    /dev/go/pkg/mod/github.com/freeconf/yang@v0.0.0-20240722123345-493940563c44/parser/loader.go:80 +0xe6
github.com/freeconf/yang/parser.(*parser).loadAndParseModule(0xc000048040, 0x0, {0x5b6d04, 0xf}, {0x467399?, 0x10?}, {0x608008, 0xc0000308a0}, 0xc000016140)
    /dev/go/pkg/mod/github.com/freeconf/yang@v0.0.0-20240722123345-493940563c44/parser/loader.go:110 +0x278
github.com/freeconf/yang/parser.LoadModuleWithOptions(0xc0000120a8, {0x5b6d04, 0xf}, {{0x0?, 0x0?}, {0x0?, 0xc0000061c0?}})
    /dev/go/pkg/mod/github.com/freeconf/yang@v0.0.0-20240722123345-493940563c44/parser/loader.go:68 +0x12f
github.com/freeconf/yang/parser.LoadModule(...)
    /dev/go/pkg/mod/github.com/freeconf/yang@v0.0.0-20240722123345-493940563c44/parser/loader.go:16
main.main()
    /dev/scratch/freeconf/main.go:22 +0x65

The culprit is the little // foo comment after the module definition

dhubler commented 2 weeks ago

I'd say it it legal YANG and a legit bug. Not hard to fix, but will require looking into parser and/or lexer logic and not obvious. Thanks for reporting, let me know if this is urgent.

preetjuniper commented 2 weeks ago

Not urgent, as it seems trivial to workaround. Also, another observation, loading from string doesn't panic:

func main() {
    configModelStr := `
        module x {
            namespace "";
            prefix "";
            revision 0;
        } // foo
    `

    // load model
    _, err := parser.LoadModuleFromString(nil, configModelStr)
    if err != nil {
        panic(err.Error())
    }
}