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

Go 1.16 will change behavior of what appears to be a buggy conditional #137

Closed
ianlancetaylor opened this issue Jan 11, 2021 · 0 comments · Fixed by #138
Closed

Go 1.16 will change behavior of what appears to be a buggy conditional #137

ianlancetaylor opened this issue Jan 11, 2021 · 0 comments · Fixed by #138

Comments

@ianlancetaylor
Copy link

This line of code looks buggy to me:

https://github.com/go-openapi/validate/blob/master/type.go#L140

The troublesome code is:

reflect.DeepEqual(reflect.Zero(reflect.TypeOf(data)), reflect.ValueOf(data))

The function reflect.DeepEqual takes two different values of any type and returns whether they are "deep equal". It does not ordinarily take two different values of the type reflect.Value, because it is not normally interesting whether two different reflect.Value values are equal. What is interesting is whether the values stored in the reflect.Value values are equal.

Before Go 1.16, a call to reflect.Zero would internally store a pointer to a newly allocated zero value. Thus before Go 1.16 this expression would always return false (at least, I can't think of any cases where it would return true). In Go 1.16 that was changed by https://golang.org/cl/192331: as an optimization, reflect.Zero will now use the same pointer for all types smaller than 1024 bytes. The effect is that now if data was itself obtained by a call to reflect.Zero, the expression will now return true.

It appears that the code is trying to do something like

reflect.DeepEqual(reflect.Zero(reflect.TypeOf(data)).Interface(), data)

which as of Go 1.13 is more efficiently written as

reflect.ValueOf(data).IsZero()

I don't know what the right fix is here. Changing this code has various knock-on effects on other packages that use this one. Is an empty string really the same as a null value? I don't know.

anitgandhi added a commit to digitalocean/validate that referenced this issue Feb 25, 2021
… v0.19.8

As reported in go-openapi#137, changes in Go 1.16's `reflect` package cause the type validator to fail now. Incorporate a fix from go-openapi#138 to fix that.

For the time being, for internal compatibility and transitive dependency issues, we need to stick with v0.19.8. But to unblock upgrading to Go 1.16.0, we need this hotfix.
tamalsaha added a commit to kmodules/codespan-schema-checker that referenced this issue Mar 9, 2021
ref: go-openapi/validate#137

Signed-off-by: Tamal Saha <tamal@appscode.com>
tamalsaha added a commit to appscodelabs/k8s-gomod-refresher that referenced this issue Mar 9, 2021
ref: go-openapi/validate#137

Signed-off-by: Tamal Saha <tamal@appscode.com>
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.

2 participants
@ianlancetaylor and others