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
encoding/json: Implementing Value() while using json.Marshal on Map Alias throws Panic #59302
Comments
Just to be clear, the issue can happen with the exact test code you gave above? |
@renevall As per your stack trace, you have defined a custom
|
not an issue with the Go project then. |
@rittneje @seankhliao The exact implementation is the one you see in the ticket description. The only difference between the minimal code and the one in the real code is that there are other files in the same package. But those files are unrelated since the testing code triggers the bug. The testing code as seen on the description triggers the fatal/panic. I'll give it another pass to look for more context |
@renevall Those other files are not unrelated. As per the stack trace, you defined the |
Thanks @rittneje. I ended up finding the source of the problem. I had this extra code that relates: }
func (v Versions) MarshalJSON() ([]byte, error) {
return json.Marshal(v)
} So your explanation is correct. I was creating the issue myself. Thank you so much. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
I tested both 1.20.1 and 1.20.2. It happened in both.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I created a custom type based on a map(an Alias) to store in the database as json. The code ends up looking like:
I tracked down the panic to the
return json.Marshal(v)
, getting an error that looks like:After a while I tried to reproduce the bug on the playground:
https://go.dev/play/p/SlWjiWP2FaB
But I could not reproduce it.
I then tried to run the test in my ubuntu(under wsl2) computer and the bug was showing there.
My next test was to isolate the code in my machine, new project folder, new go mod, just two files. The test runs as intended.
After that, I figure I could try creating a new package/folder under the real app, and put the two files that worked on the side project. The test worked.
So I decided to test the opposite, take all the files of the package giving the error on the real app and past it to the side project. Because this is the "domain" package, I did not need to copy any of the rest of the files of the real app.
You guessed it, the errors shows.
My guess as this point is/was that there could be some low level optimization making the json.Marshall function call the Value() function, entering an endless loop. This would trigger either by the package size or by the imported dependencies.
Finally, changing the Value() to the following bypasses the bug:
What did you expect to see?
The Value() would return the json encoded version of the map.
What did you see instead?
Output
The text was updated successfully, but these errors were encountered: