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

🐛 returned objects of reference type should be unchangeable #1851

Merged
merged 1 commit into from
Apr 1, 2022

Conversation

mars1024
Copy link
Contributor

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.

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Mar 29, 2022
@mars1024
Copy link
Contributor Author

/retest pull-controller-runtime-test-master

@k8s-ci-robot
Copy link
Contributor

@mars1024: The /retest command does not accept any targets.
The following commands are available to trigger required jobs:

  • /test pull-controller-runtime-test-master

The following commands are available to trigger optional jobs:

  • /test pull-controller-runtime-apidiff-master

Use /test all to run all jobs.

In response to this:

/retest pull-controller-runtime-test-master

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.

@mars1024
Copy link
Contributor Author

/test pull-controller-runtime-test-master

@FillZpp
Copy link
Contributor

FillZpp commented Mar 30, 2022

/lgtm
/cc @alvaroaleman

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 30, 2022
@@ -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
Copy link
Member

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.

Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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).

Copy link
Member

@alvaroaleman alvaroaleman Mar 31, 2022

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?

Copy link
Contributor Author

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 ~

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed lgtm "Looks good to me", indicates that a PR is ready to be merged. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Apr 1, 2022
@mars1024
Copy link
Contributor Author

mars1024 commented Apr 1, 2022

test case added, PTAL @alvaroaleman @FillZpp , thanks!

Copy link
Member

@alvaroaleman alvaroaleman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 1, 2022
@k8s-ci-robot
Copy link
Contributor

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Apr 1, 2022
@k8s-ci-robot k8s-ci-robot merged commit 05aa087 into kubernetes-sigs:master Apr 1, 2022
@k8s-ci-robot k8s-ci-robot added this to the v0.10.x milestone Apr 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants