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

gogrep panics with "func $x()" pattern #36

Closed
quasilyte opened this issue Sep 2, 2018 · 4 comments

Comments

Projects
None yet
2 participants
@quasilyte
Copy link
Contributor

commented Sep 2, 2018

**Reproducer: **

  1. cd $GOROOT/src
  2. gogrep -x 'func $x()' ./...

Result:

gogrep -x 'func $x()' ./...
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x65a6f1]

goroutine 1 [running]:
main.(*matcher).node(0xc00008add0, 0x750fc0, 0x0, 0x750fc0, 0xc00025e4e0, 0x1)
	$GOPATH/src/mvdan.cc/gogrep/match.go:513 +0x2101
main.(*matcher).node(0xc00008add0, 0x751440, 0xc000084e40, 0x751440, 0xc00025e510, 0x0)
	$GOPATH/src/mvdan.cc/gogrep/match.go:440 +0x32d6
main.(*matcher).topNode(0xc00008add0, 0x751440, 0xc000084e40, 0x751440, 0xc00025e510, 0x0, 0x0)
	$GOPATH/src/mvdan.cc/gogrep/match.go:264 +0xad
main.(*matcher).cmdRange.func1(0x751440, 0xc000084e40, 0x751440, 0xc00025e510)
	$GOPATH/src/mvdan.cc/gogrep/match.go:106 +0xce
main.(*matcher).walkWithLists.func1(0x751440, 0xc00025e510, 0x751601)
	$GOPATH/src/mvdan.cc/gogrep/match.go:242 +0x7c
go/ast.inspector.Visit(0xc01991a120, 0x751440, 0xc00025e510, 0x7506e0, 0xc01991a120)
	$GOROOT/src/go/ast/walk.go:373 +0x3a
go/ast.Walk(0x7506e0, 0xc01991a120, 0x751440, 0xc00025e510)
	$GOROOT/src/go/ast/walk.go:52 +0x66
go/ast.walkDeclList(0x7506e0, 0xc01991a120, 0xc00025cc80, 0x4, 0x4)
	$GOROOT/src/go/ast/walk.go:38 +0x9e
go/ast.Walk(0x7506e0, 0xc01991a120, 0x7513c0, 0xc000283a00)
	$GOROOT/src/go/ast/walk.go:353 +0x2656
go/ast.Inspect(0x7513c0, 0xc000283a00, 0xc01991a120)
	$GOROOT/src/go/ast/walk.go:385 +0x4b
main.inspect(0x7513c0, 0xc000283a00, 0xc01991a120)
	$GOPATH/src/mvdan.cc/gogrep/main.go:279 +0x162
main.(*matcher).walkWithLists(0xc00008add0, 0x751440, 0xc000084e40, 0x7513c0, 0xc000283a00, 0xc0193a56b0)
	$GOPATH/src/mvdan.cc/gogrep/match.go:254 +0x93
main.(*matcher).cmdRange(0xc00008add0, 0x6ffc5e, 0x1, 0x7ffe8b993097, 0x9, 0x6b6760, 0xc000084e40, 0xc014b23900, 0xd, 0xd, ...)
	$GOPATH/src/mvdan.cc/gogrep/match.go:121 +0x194
main.(*matcher).cmdRange-fm(0x6ffc5e, 0x1, 0x7ffe8b993097, 0x9, 0x6b6760, 0xc000084e40, 0xc014b23900, 0xd, 0xd, 0x6e8be0, ...)
	$GOPATH/src/mvdan.cc/gogrep/match.go:70 +0x79
main.(*matcher).submatches(0xc00008add0, 0xc000084cf0, 0x1, 0x1, 0xc014b23900, 0xd, 0xd, 0xc01867bdc8, 0xc0193a53b0, 0x1c)
	$GOPATH/src/mvdan.cc/gogrep/match.go:89 +0x136
main.(*matcher).matches(0xc00008add0, 0xc000084cf0, 0x1, 0x1, 0xc0000f6400, 0xd, 0x10, 0xc00000e000, 0xc00bbce000, 0x11e)
	$GOPATH/src/mvdan.cc/gogrep/match.go:24 +0x1df
main.(*matcher).fromArgs(0xc00008add0, 0xc00000e0d0, 0x3, 0x3, 0xc000055f88, 0x405539)
	$GOPATH/src/mvdan.cc/gogrep/main.go:164 +0x3ca
main.main()
	$GOPATH/src/mvdan.cc/gogrep/main.go:63 +0xe1

If gogrep doesn't support such patterns, there should be something like pattern-compile error. Descriptive error message would help.

@quasilyte

This comment has been minimized.

Copy link
Contributor Author

commented Sep 2, 2018

It also panics for func $x() {}.
Need to check those stack traces..

@quasilyte

This comment has been minimized.

Copy link
Contributor Author

commented Sep 2, 2018

In func $x() both x and y in m.cases(x.List, y.List) are nil.
In func $x() {} only y is nil *ast.BlockStmt.
Accessing .List causes panic.

Interestingly, func $x() {} is matched as *FuncDecl.

x.Body and y.Body can be nil, so there should probably some additional care in handling them, like checking for nils, for example (not sure how such cases are handled in other parts of gogrep, so just guessing).

@mvdan

This comment has been minimized.

Copy link
Owner

commented Sep 9, 2018

Apologies for the late reply - was on a week-long holiday. Still working through my backlog of stuff at the moment.

@mvdan mvdan closed this in a838822 Mar 6, 2019

@quasilyte

This comment has been minimized.

Copy link
Contributor Author

commented Mar 6, 2019

Thank you for solving this. ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.