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

panic when passing context AND nil in a table Entry #1415

Open
nunnatsa opened this issue May 13, 2024 · 1 comment
Open

panic when passing context AND nil in a table Entry #1415

nunnatsa opened this issue May 13, 2024 · 1 comment

Comments

@nunnatsa
Copy link
Contributor

On DescribeTable when adding a context parameter, and a pointer parameter, if the parameter is nil, the test exit with panic.

This example reproduces the issue (note: this is the simplest reproduction I found. This is why the context is not in use; but this is happening also if the context is in use).

func f(num *int) string {
	if num == nil {
		return ""
	}
	return strconv.Itoa(*num)
}

var _ = Describe("some text", func() {
	DescribeTable("table with ctx", func(ctx context.Context, num *int, s string) {
		Expect(f(num)).To(Equal(s))
	},
		Entry("nil", nil, ""),
		Entry("4", &four, "4"),
		Entry("5", &five, "5"),
		Entry("-6", &six, "-6"),
	)
})

When running the test, it fails for building the tree:

Assertion or Panic detected during tree construction
DescribeTable("table with ctx", func(ctx context.Context, num *int, s string) {
/home/nunnatsa/GIT/ginkgo-test/tests/ttt_test.go:31
  Ginkgo detected a panic while constructing the spec tree.
  You may be trying to make an assertion in the body of a container node
  (i.e. Describe, Context, or When).

  Please ensure all assertions are inside leaf nodes such as BeforeEach,
  It, etc.

  Here's the content of the panic that was caught:
  runtime error: invalid memory address or nil pointer dereference

  Learn more at: http://onsi.github.io/ginkgo/#no-assertions-in-container-nodes

Notes:

  • This is not happening if num is not a pointer.
  • This is not happening if there is no context parameter.

Additional info:

The cause is the nil parameter in the first Entry.

The panic is from here:

} else if internalBodyType.In(0).Implements(contextType) && (len(entry.parameters) == 0 || !reflect.TypeOf(entry.parameters[0]).Implements(contextType)) {

entry.parameters[0] is nil, so reflect.TypeOf(entry.parameters[0]) is also nil, and then reflect.TypeOf(entry.parameters[0]).Implements(contextType)) panics.

@onsi
Copy link
Owner

onsi commented May 21, 2024

hey @nunnatsa thanks for reporting this. i have a fix on master now and will cut a release soon

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