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

A bug that mutates a not-used copy of a struct #479

Closed
shivanshuraj1333 opened this issue Oct 4, 2021 · 0 comments · Fixed by #480
Closed

A bug that mutates a not-used copy of a struct #479

shivanshuraj1333 opened this issue Oct 4, 2021 · 0 comments · Fixed by #480

Comments

@shivanshuraj1333
Copy link
Contributor

At https://github.com/emicklei/go-restful/blob/v3/route.go#L176-L178
Here Route is struct which will be copied by value. The mutation on the value receiver r doesn't do anything to the original struct in the caller. As a consequence, this mutation has no meaningful effect.

type Route struct {
	Method   string
	...
	contentEncodingEnabled *bool
}

func (r Route) EnableContentEncoding(enabled bool) {
	r.contentEncodingEnabled = &enabled
}

One fix is to use pointer receiver.

func (r *Route) EnableContentEncoding(enabled bool) {
	r.contentEncodingEnabled = &enabled
}

Another possible fix is to write back enabled into the caller'scontentEncodingEnabled :

func (r Route) EnableContentEncoding(enabled bool) {
	*r.contentEncodingEnabled = enabled
}

How to reproduce it (as minimally and precisely as possible):
A simplified bug reproducer is available at: https://play.golang.org/p/cibNCyIarZS

Initially filled in Kubernetes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant