go-piv / piv-go

Keys and certificates for YubiKeys, written in Go
Apache License 2.0
368 stars 65 forks source link

Fix panic when k.begin() attempts to return early #32

Closed dnesting closed 4 years ago

dnesting commented 4 years ago

https://github.com/go-piv/piv-go/blob/master/piv/key.go#L537-L541

The defer calls tx.Close() when needed to clean up, however all of the early return paths explicitly return nil for tx, causing a nil dereference panic. This PR has two commits, one with the test case proving the panic and the other with a fix.

Another approach could be setting err = fmt.Errorf(...) and just doing a bare return at the end.

ericchiang commented 4 years ago

Thanks!