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

WithinArshalCall check does not work for nested calls #162

Open
dsnet opened this issue Feb 12, 2025 · 0 comments
Open

WithinArshalCall check does not work for nested calls #162

dsnet opened this issue Feb 12, 2025 · 0 comments

Comments

@dsnet
Copy link
Collaborator

dsnet commented Feb 12, 2025

There's an internal WithinArshalCall flag that the "json" implementation sets whenever calling a user-provided marshaler or unmarshaler. The purpose of this flag is as a sanity check that causes Encoder.Reset and Decoder.Reset to panic.

This check doesn't work for nested marshaler calls since inner call will clear the flag. We should either drop the check (since it's only as a sanity protection) or fix it such that it only clears the flag if it was able to set it in the first place.

Thus instead of:

json/arshal_methods.go

Lines 195 to 197 in 60a0516

xe.Flags.Set(jsonflags.WithinArshalCall | 1)
err := va.Addr().Interface().(MarshalerTo).MarshalJSONTo(enc, mo)
xe.Flags.Set(jsonflags.WithinArshalCall | 0)

It'd be something like:

if !xe.Flags.Get(jsonflags.WithinArshalCall) {
     xe.Flags.Set(jsonflags.WithinArshalCall | 1) 
     defer func() { xe.Flags.Set(jsonflags.WithinArshalCall | 0) }
}
err := va.Addr().Interface().(MarshalerTo).MarshalJSONTo(enc, mo) 

Overall, this is a pretty minor bug.

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

No branches or pull requests

1 participant