Skip to content

fix: handle interface destinations holding pointers or requiring methods - #152

Merged
shamaton merged 2 commits into
mainfrom
fix/interface-destination
Nov 8, 2026
Merged

shamaton merged 2 commits into
mainfrom
fix/interface-destination

Conversation

@shamaton

@shamaton shamaton commented Nov 5, 2026

Copy link
Copy Markdown
Owner

Summary

  • Fix the stream decoder reading past the value when an interface destination holds a pointer
  • Fix the panic or EOF when an interface destination holds a typed nil pointer
  • Return an error instead of panicking for non-empty interface destinations (error, fmt.Stringer, ...)

Background

These bugs predate v3 and also exist in v2. They only appear with these destination types:

  • Interface holding a non-nil pointer: UnmarshalRead read the next byte instead of reusing the code byte it had already read. That gave EOF, or the wrong value when more data followed.
  • Interface holding a typed nil pointer: Unmarshal panicked and UnmarshalRead returned EOF.
  • Non-empty interface such as an error field: both decoders panicked in reflect.Set when the value did not implement the interface. Round-tripping a struct with a non-nil error field hits this.

Changes

  • Decode into the held pointer only when it is non-nil. Otherwise replace the interface value, matching encoding/json.
  • Check that the value implements a non-empty interface before setting it, and return an error wrapping ErrCanNotDecode if it does not.
  • interface{} destinations skip the check.
  • Regression tests for both decoders.

Test Plan

  • go vet ./... and go test -race ./...
  • The new tests fail on main (8 cases) and pass with this change
  • Interleaved benchmarks against main:
    • No change in allocations.
    • Up to +3.6% on decoding 1000 small ints into []interface{}, about 0.8ns per element, from the empty-interface type check.
    • Other cases show no significant change.

Notes

  • The empty-interface check is a type comparison that the compiler can't fully inline without unsafe, and this library avoids unsafe. Is the small per-element cost acceptable?

When the destination interface already held a pointer, the stream decoder
read the next byte instead of reusing the code byte it had already read,
failing with EOF or decoding the wrong value. A typed nil pointer held by
the interface made the buffer decoder panic and the stream decoder fail.
Decode into the held pointer only when it is non-nil, reusing the code byte
in the stream decoder, and otherwise replace the interface value.
…tinations

Decoding into an interface with methods, such as error or fmt.Stringer,
set the decoded value with reflect.Set, which panics when the value does
not implement the interface. Check the value first and return an error
wrapping ErrCanNotDecode. interface{} destinations skip the check.
@github-actions github-actions Bot added the type: bug Confirmed or likely defect label Nov 5, 2026
@codecov

codecov Bot commented Nov 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.82%. Comparing base (8213ebf) to head (6dfc599).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #152      +/-   ##
==========================================
+ Coverage   92.78%   92.82%   +0.04%     
==========================================
  Files          75       75              
  Lines        7189     7203      +14     
==========================================
+ Hits         6670     6686      +16     
+ Misses        319      318       -1     
+ Partials      200      199       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@shamaton
shamaton merged commit 083d4a3 into main Nov 8, 2026
27 checks passed
@shamaton
shamaton deleted the fix/interface-destination branch November 8, 2026 05:38
@github-actions github-actions Bot mentioned this pull request Nov 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug Confirmed or likely defect

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant