Skip to content

Do and DoAndReturn panic when a variadic argument is nil #315

Description

@lenamonj

Actual behavior A mocked variadic method whose expectation carries a Do or DoAndReturn function panics when the code under test passes nil as a variadic element. Both actions synthesize the zero value for a nil argument from ft.In(i), where i counts the arguments flattened as the mock passes them, but a variadic func type holds the slice at its last position and nothing beyond it. At the last position the zero value is the slice itself and reflect.Call panics with reflect: cannot use []error as type error in Call; past it, ft.In(i) panics with index out of range.

Expected behavior The function receives nil in that variadic position, the same as it does for a nil fixed argument.

To Reproduce Steps to reproduce the behavior

  1. Build a Call for func(string, ...error) and attach Do(func(_ string, errs ...error) {}).
  2. Run the action with the arguments "msg", nil, nil.
  3. The test binary panics instead of calling the function with two nil errors.

TestCall_Do_NilVariadicArg and TestCall_DoAndReturn_NilVariadicArg in the branch below do exactly this and fail on main at d8c3a2a.

Additional Information

  • gomock mode (reflect or source): both, the failure is in gomock/call.go
  • gomock version or git ref: main at d8c3a2a
  • golang version: go1.26.2

Triage Notes for the Maintainers

I have a fix with the two tests ready at lenamonj@17c9aee: an argTypeAt helper that folds every position from the variadic one onwards onto the element type, used at both zero-value sites. go vet, go build, ci/test.sh, ci/check_panic_handling.sh and the module tests pass on it. CONTRIBUTING asks for an issue before a pull request, so I am opening this first and will open the pull request if you want the change.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions