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

Add nil check when applying variadic client options #806

Closed
rajathagasthya opened this issue Feb 20, 2020 · 9 comments
Closed

Add nil check when applying variadic client options #806

rajathagasthya opened this issue Feb 20, 2020 · 9 comments
Labels
good first issue Denotes an issue ready for a new contributor, according to the "help wanted" guidelines. help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. priority/backlog Higher priority than priority/awaiting-more-evidence.
Milestone

Comments

@rajathagasthya
Copy link
Contributor

All of the Create, List, Update and Delete client methods are variadic in that it takes any number of options. For example:

Create(ctx context.Context, obj runtime.Object, opts ...client.CreateOption) error

It's too easy to pass nil as the last argument for these methods and then it blows up with a NPE in ApplyOptions method. Example:

opt.ApplyToCreate(o)

There needs to be a nil check when options are applied.

@vincepri
Copy link
Member

/kind cleanup
/milestone v0.5.x
/help
/good-first-issue
/priority backlog

@k8s-ci-robot
Copy link
Contributor

@vincepri:
This request has been marked as suitable for new contributors.

Please ensure the request meets the requirements listed here.

If this request no longer meets these requirements, the label can be removed
by commenting with the /remove-good-first-issue command.

In response to this:

/kind cleanup
/milestone v0.5.x
/help
/good-first-issue
/priority backlog

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. good first issue Denotes an issue ready for a new contributor, according to the "help wanted" guidelines. priority/backlog Higher priority than priority/awaiting-more-evidence. help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. labels Feb 21, 2020
@vincepri vincepri added this to the v0.5.x milestone Feb 21, 2020
@boddumanohar
Copy link

@rajathagasthya its not possible to pass nil as a last argument to variadic function

package main

import (
	"fmt"
)

func printStrings(s ...string) {
	for _, s := range s {
		fmt.Println(s)
	}
}

func main() {
	printStrings("alice", "bob", nil)
}

https://play.golang.org/p/8iOacL4kOvX

this example will throw an error.

./prog.go:14:24: cannot use nil as type string in argument to printStrings

@rajathagasthya
Copy link
Contributor Author

rajathagasthya commented Feb 21, 2020

@boddumanohar CreateOption (and its variants) are interfaces, so it's possible to pass nil here.

https://play.golang.org/p/AZWZJZ7-ZAs

@boddumanohar
Copy link

created a PR for this.

@alvaroaleman
Copy link
Member

Repeating my comment from the PR in #813 , IMHO we shouldn't be doing this:

/hold
Actually stop, I don't think we should be doing this, because methods of a nullpointer are still callable and we have a couple of examples where this works without issue, e.G. MatchingLabels, HasLabels, MatchingFields. Those examples here are a no-op if nil, but there could be examples out there where this is not the case. With this change, we silently stop applying them which is surprising at best.

IMHO a method that does not explicitly state that it may be called with nil args can expect the caller to make sure that this doesn't happen.

@alvaroaleman
Copy link
Member

@rajathagasthya WDYT, does that make sense?

@rajathagasthya
Copy link
Contributor Author

Discussed in #813. Closing based on preference not to add this check.

@rajathagasthya
Copy link
Contributor Author

/close

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Denotes an issue ready for a new contributor, according to the "help wanted" guidelines. help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. priority/backlog Higher priority than priority/awaiting-more-evidence.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants