Safer behaviour of (mis)logging badly coded marshallers#1505
Open
alshopov wants to merge 1 commit intouber-go:masterfrom
Open
Safer behaviour of (mis)logging badly coded marshallers#1505alshopov wants to merge 1 commit intouber-go:masterfrom
alshopov wants to merge 1 commit intouber-go:masterfrom
Conversation
ArrayMarshalerType, ObjectMarshalerType, InlineMarshalerType all call user provided methods on user provided values. They may fail/panic and have to be guarded. Use guard similar to the one for stringer with the following caveats: - ObjectMarshalerType in case of nil-ness will be logged as string rather than map. - ArrayMarshalerType - we have to insert a synthetic empty marshaller to log an empty slice. Otherwise the default shape of map shines through - InlineMarshalerType - in case of any error it will be de-inlined to show exact cause of error. Otherwise error will be attached to the wrapping object which will make debugging and fixing harder While we introduce enough coverage, the test harness is not using currently subtests. This patch does not address this to keep changes to a minimum and give time to discuss if this is necessary. Thus there are 3 tests that were changed - the error cases, the success cases and inline case (separate).
Contributor
Author
|
@JacobOaks, @tchung1118: as discussed on #1501, I am proposing a fix for #1504. |
Contributor
Author
|
@prashantv Have a look at the implementation I propose for fixing problems you outlined here: #1501 (comment) |
tchung1118
reviewed
Jul 11, 2025
There was a problem hiding this comment.
Can we also add benchmarks to compare performances of before and after this change?
(on behalf of @sywhang from group code review)
| enc.AddString(key, "<nil>") | ||
| return | ||
| } | ||
| _ = enc.AddArray(key, emptyArrayMarshaler{}) |
Comment on lines
+244
to
+247
| if v := reflect.ValueOf(objectMarshaller); v.Kind() == reflect.Ptr && v.IsNil() { | ||
| enc.AddString(key, "<nil>") | ||
| return | ||
| } |
There was a problem hiding this comment.
It doesn't seem necessary to log nil here separately because the error returned from here will already indicate there's a nil panic without this. Let's not do this because reflection is expensive.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ArrayMarshalerType, ObjectMarshalerType, InlineMarshalerType all call
user provided methods on user provided values.
They may fail/panic and have to be guarded.
Use guard similar to the one for stringer with the following caveats:
Otherwise the default shape of map shines through
Otherwise error will be attached to the wrapping object which will make debugging and fixing harder
While we introduce enough coverage, the test harness is not using currently subtests.
This patch does not address this to keep changes to a minimum and give time to discuss if this is necessary.
Thus there are 3 tests that were changed - the error cases, the success cases and inline case (separate).