-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
🐛 returned objects of reference type should be unchangeable #1851
Conversation
/retest pull-controller-runtime-test-master |
@mars1024: The
The following commands are available to trigger optional jobs:
Use In response to this:
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. |
/test pull-controller-runtime-test-master |
/lgtm |
@@ -193,8 +193,8 @@ func indexByField(indexer Informer, field string, extractor client.IndexerFunc) | |||
rawVals := extractor(obj) | |||
var vals []string | |||
if ns == "" { | |||
// if we're not doubling the keys for the namespaced case, just re-use what was returned to us | |||
vals = rawVals | |||
// if we're not doubling the keys for the namespaced case, just create a new slice with same length |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I don't understand. If you write a function that returns a pointer, you are not supposed to keep a reference to that pointer or its underlying value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you clarify why you are doing that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The story is, I compute and put the indexer values into the object status .status.indexerValues
, so the extractor will be
func(obj client.Object) []string {
someObject, _ := obj.(*group.ObjectKind)
return someObject.Status.IndexerValues
}
As we know, the object from client cache should be unchangeable.
Just think another scene,
values := []string{"a", "b", "c"}
do(values)
doAgain(values)
Do we expect values
will be changed by do
? Then doAgain
will get unexpected values.
Though slice(extractor return) is a pointer type here, but it is used because it contains immutable values, so I don't think it should be changed when being used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Slice(extractor return) here is more like a container containing immutable values(indexer values).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, can you then please add a test that covers this scenario?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course, will do it ~
Signed-off-by: Bruce Ma <[email protected]>
test case added, PTAL @alvaroaleman @FillZpp , thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: alvaroaleman, mars1024 The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Signed-off-by: Bruce Ma [email protected]
The returned slice of
extractor
function is reference type, reusing it will bring unpredictable problems, it should be unchangeable.