Skip to content

fix(encoder): guard the IsZeroer check with CanInterface in omitempty - #970

Open
prostomarkeloff wants to merge 1 commit into
goccy:masterfrom
prostomarkeloff:fix-omitempty-caninterface
Open

prostomarkeloff wants to merge 1 commit into
goccy:masterfrom
prostomarkeloff:fix-omitempty-caninterface

Conversation

@prostomarkeloff

Copy link
Copy Markdown

Found with find-dup-defs using its converge pass, which flagged isOmittedByOmitZero and isOmittedByOmitEmptyTag as near-duplicate helpers diverging on one guard.

encode.go isOmittedByOmitEmptyTag calls v.Interface().(IsZeroer) directly. Its twin isOmittedByOmitZero wraps the same call in if v.CanInterface(). A value reached by traversing an embedded unexported struct field is not interfaceable, so yaml.Marshal of such a type tagged ,omitempty panics with reflect: cannot return value obtained from unexported field or method. The omitzero sibling marshals the same type fine, which is what pointed at the missing guard.

Fix: wrap the IsZeroer assertion in if v.CanInterface(), mirroring isOmittedByOmitZero.

Added a regression test marshaling a struct that embeds an unexported type tagged ,omitempty; it panics before the change and passes after.

Disclosure: prepared with the help of an AI tool; I have reviewed the change and test and understand each line.

isOmittedByOmitEmptyTag called v.Interface().(IsZeroer) without the
`if v.CanInterface()` guard its twin isOmittedByOmitZero has, so Marshal
panicked on a struct embedding an unexported type tagged `,omitempty`
(reflect: cannot return value obtained from unexported field or method).
Wrap the assertion in CanInterface, mirroring the omitzero path.
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 20.00000% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.66%. Comparing base (edee2f9) to head (24028af).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #970   +/-   ##
=======================================
  Coverage   80.65%   80.66%           
=======================================
  Files          22       22           
  Lines        6845     6846    +1     
=======================================
+ Hits         5521     5522    +1     
  Misses        905      905           
  Partials      419      419           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
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 this pull request may close these issues.

2 participants