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

Parsing from file fails with comment after module definition #118

Open
preetjuniper opened this issue Aug 29, 2024 · 2 comments
Open

Parsing from file fails with comment after module definition #118

preetjuniper opened this issue Aug 29, 2024 · 2 comments

Comments

@preetjuniper
Copy link

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
Copy link
Collaborator

dhubler commented Aug 29, 2024

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
Copy link
Author

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())
	}
}

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

No branches or pull requests

2 participants