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

Fixed using reference in for loop #105459

Merged
merged 1 commit into from
Nov 13, 2021

Conversation

shivanshuraj1333
Copy link
Contributor

@shivanshuraj1333 shivanshuraj1333 commented Oct 4, 2021

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

Current code incorrectly uses for loop and passes wrong value as reference
Guide to refer https://github.com/golang/go/wiki/CommonMistakes#using-reference-to-loop-iterator-variable

Which issue(s) this PR fixes:

Fixes #105426
Fixes #105425

Special notes for your reviewer:

Please refer to sample code to understand the bug https://goplay.space/#TIT_IZEAEud

package main

import "fmt"

type st struct {
	it []string
}

func main() {
	rp := &st{[]string{"value1", "value2"}}
	ownedReplicaSets1 := make([]*string, 0, len(rp.it))
	ownedReplicaSets2 := make([]*string, 0, len(rp.it))

	// similar to current implementation
	for _, t := range rp.it {
		ownedReplicaSets1 = append(ownedReplicaSets1, &t)
	}

	fmt.Println(ownedReplicaSets1)
	fmt.Println(*ownedReplicaSets1[0], *ownedReplicaSets1[1])

	// similar new change
	for i := range rp.it {
		ownedReplicaSets2 = append(ownedReplicaSets2, &rp.it[i])
	}

	fmt.Println(ownedReplicaSets2)
	fmt.Println(*ownedReplicaSets2[0], *ownedReplicaSets2[1])
}
output:
value2 value2
[0xc000054020 0xc000054030]
value1 value2

Does this PR introduce a user-facing change?

NONE

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Oct 4, 2021
@k8s-ci-robot
Copy link
Contributor

@shivanshu1333: This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

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 do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Oct 4, 2021
@k8s-ci-robot k8s-ci-robot added area/test sig/storage Categorizes an issue or PR as relevant to SIG Storage. sig/testing Categorizes an issue or PR as relevant to SIG Testing. sig/autoscaling Categorizes an issue or PR as relevant to SIG Autoscaling. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Oct 4, 2021
@shivanshuraj1333
Copy link
Contributor Author

/retest

@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Oct 7, 2021
@shivanshuraj1333
Copy link
Contributor Author

/retest-required

@shivanshuraj1333
Copy link
Contributor Author

/retest

@shivanshuraj1333
Copy link
Contributor Author

@aojea this PR is similar to #105433, do you want to take a look at this as well?
Thanks!!

@shivanshuraj1333
Copy link
Contributor Author

@gnufied PTAL

@shivanshuraj1333
Copy link
Contributor Author

shivanshuraj1333 commented Oct 26, 2021

ping @aojea :)

@aojea
Copy link
Member

aojea commented Oct 26, 2021

I'd like for the people on those SIGs to review first, I wonder why the tests weren't affected
/assign @pohly for storage
ping @sig-autoscaling-maintainers

@k8s-ci-robot
Copy link
Contributor

@aojea: GitHub didn't allow me to assign the following users: for, storage.

Note that only kubernetes members, repo collaborators and people who have commented on this issue/PR can be assigned. Additionally, issues/PRs can only have 10 assignees at the same time.
For more information please see the contributor guide

In response to this:

I'd like for the people on those SIGs to review first, I wonder why the tests weren't affected
/assign @pohly for storage
ping @sig-autoscaling-maintainers

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.

@shivanshuraj1333
Copy link
Contributor Author

ping @pohly

@shivanshuraj1333
Copy link
Contributor Author

/retest

test/e2e/autoscaling/cluster_size_autoscaling.go Outdated Show resolved Hide resolved
test/e2e/autoscaling/cluster_size_autoscaling.go Outdated Show resolved Hide resolved
test/e2e/autoscaling/cluster_size_autoscaling.go Outdated Show resolved Hide resolved
test/e2e/autoscaling/cluster_size_autoscaling.go Outdated Show resolved Hide resolved
@k8s-ci-robot k8s-ci-robot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Nov 12, 2021
@shivanshuraj1333
Copy link
Contributor Author

ping @pohly for approval

Copy link
Contributor

@pohly pohly left a comment

Choose a reason for hiding this comment

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

/lgtm
/retest

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 12, 2021
@pohly
Copy link
Contributor

pohly commented Nov 12, 2021

/assign @bskiba

For approval as suggested by bot.

@aojea
Copy link
Member

aojea commented Nov 12, 2021

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: aojea, pohly, shivanshu1333

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 Nov 12, 2021
@shivanshuraj1333
Copy link
Contributor Author

/retest

@k8s-ci-robot k8s-ci-robot merged commit c1b9ada into kubernetes:master Nov 13, 2021
@k8s-ci-robot k8s-ci-robot added this to the v1.23 milestone Nov 13, 2021
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. area/test cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. release-note-none Denotes a PR that doesn't merit a release note. sig/autoscaling Categorizes an issue or PR as relevant to SIG Autoscaling. sig/storage Categorizes an issue or PR as relevant to SIG Storage. sig/testing Categorizes an issue or PR as relevant to SIG Testing. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
5 participants