onsi / gomega

Ginkgo's Preferred Matcher Library
http://onsi.github.io/gomega/
MIT License
2.19k stars 284 forks source link

WithTransform of nil error panics #420

Closed cbandy closed 3 years ago

cbandy commented 3 years ago

The following panics in v1.10.5:

var err error
gomega.WithTransform(
    func(error) bool { return true },
    gomega.BeTrue(),
).Match(err)
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x56b58b]

goroutine 1 [running]:
github.com/onsi/gomega/matchers.(*WithTransformMatcher).Match(0xc00005e380, 0x0, 0x0, 0x7d5a18, 0xc00005e380, 0xc00006a058)
        /home/cbandy/go/pkg/mod/github.com/onsi/gomega@v1.10.5/matchers/with_transform.go:45 +0x6b
main.main()

I encountered it while using Eventually() to watch for a specific error, e.g.

Eventually(func () error { ... }).Should(WithTransform(os.IsNotExist, BeTrue()))
ghost commented 3 years ago

Looks like WithTransform doesn't handle the actual being nil: https://github.com/onsi/gomega/blob/c4793565eb31535212457ff77a4a7d5b4034f405/matchers/with_transform.go#L42-L45

onsi commented 3 years ago

sounds right - @jim-slattery-rs are you up for and have time to work on a PR to cover this case?

ghost commented 3 years ago

Sure thing, I've created a PR: https://github.com/onsi/gomega/pull/422