-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
protoc-gen-go: oneof getters panic on typed-nil values. #1415
Comments
This, so far as I know, is working as intended; |
I guess that makes sense, if the API consider typed-nil in a oneof field as an error. I don't think its easy to discover this fact though. The generated code developer reference section for oneof fields doesn't mention it at all. In fact the wording could reasonably be interpreted that any nil value would return a zero value on the getters.
https://developers.google.com/protocol-buffers/docs/reference/go-generated#oneof |
Hm. Coming from the last common on the linked possibly relevant issue:
It seems we should be doing the additional |
FWIW, reflection works with this situation just fine and treats the typed-nil as if it were a nil interface -- the oneof is treated as if unset: https://go.dev/play/p/fFn0Dj6cjR7. The So, for consistency, updating the generated accessors to behave like Also, interestingly, if just using reflection, it is impossible to detect if a message is incorrectly constructed this way. In order to generically check for such malformed messages, it would be nice to remedy this, such as an |
What version of protobuf and what language are you using?
Golang
What did you do?
Panic occurs when you set a typed nil on a oneof field
What did you expect to see?
Empty string
What did you see instead?
Possibly related: #478
Anything else we should know about your project / environment?
I tracked down the source of the panic. The generated getter for the oneof field does a nil check on the interface value, but not the Str value after casting it. Following code was generated with the above tool versions.
In the typed-nil case, the cast works and reports true, so the code attempts to access
x.Str
, but x is in fact nil. There should be an additional nil check onx
.I imagine this case is probably not intended to come about in practice, but it is certainly possible.
The text was updated successfully, but these errors were encountered: