aluttik / go-crossplane

An unofficial Go port of the NGINX config/JSON converter crossplane
Apache License 2.0
50 stars 15 forks source link

util.go has a bug e should be minus one if len(d.Args[0]) == 0 #12

Closed lixiangzzz2017 closed 3 years ago

lixiangzzz2017 commented 3 years ago

when nginx conf like : "if ( $forward = "pre-release" ) {" func prepareIfArgs(d Directive) Directive { e := len(d.Args) - 1 if len(d.Args) > 0 && strings.HasPrefix(d.Args[0], "(") && strings.HasSuffix(d.Args[e], ")") { d.Args[0] = strings.TrimLeftFunc(strings.TrimPrefix(d.Args[0], "("), unicode.IsSpace) d.Args[e] = strings.TrimRightFunc(strings.TrimSuffix(d.Args[e], ")"), unicode.IsSpace) if len(d.Args[0]) == 0 { // here d.Args need to minus one d.Args = d.Args[1:] } if len(d.Args[e]) == 0 { d.Args = d.Args[:e] } } return d } or else. program will panic and like this: panic: runtime error: index out of range [4] with length 4

goroutine 1 [running]: github.com/aluttik/go-crossplane.prepareIfArgs(0xc0000bcdee, 0x2, 0x168, 0xc0001e6090, 0x4, 0x7, 0x0, 0x0, 0x0, 0x0, ...) /Users/lixiang/go/pkg/mod/github.com/aluttik/go-crossplane@v0.0.0-20201125234241-e4532485989f/util.go:60 +0x88b github.com/aluttik/go-crossplane.(*parser).parse(0xc000071b08, 0xc0001000a0, 0xc0001020c0, 0xc0001cc3a0, 0x2, 0x2, 0x0, 0x2, 0x0, 0x0, ...)

andrerfcsantos commented 3 years ago

Hey!

Just submitted a PR to fix this.