Skip to content

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

Closed
@shivanshuraj1333

Description

@shivanshuraj1333

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @shivanshuraj1333

      Issue actions

        A bug that mutates a not-used copy of a struct · Issue #479 · emicklei/go-restful