Skip to content

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

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

shamaton merged 2 commits into
v2from
fix/interface-destination-v2

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 exist in v2 and v3. 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 v2 (8 cases) and pass with this change
  • Interleaved benchmarks against v2:
    • No change in allocations.
    • +1.5% to +2.9% on []interface{} / map[interface{}]interface{} cases, about 0.6ns 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 97.87%. Comparing base (5619f59) to head (9469dae).

Additional details and impacted files
@@            Coverage Diff             @@
##               v2     #153      +/-   ##
==========================================
+ Coverage   97.84%   97.87%   +0.03%     
==========================================
  Files          74       74              
  Lines        6019     6033      +14     
==========================================
+ Hits         5889     5905      +16     
+ Misses         74       73       -1     
+ Partials       56       55       -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 b595f17 into v2 Nov 8, 2026
21 checks passed
@shamaton
shamaton deleted the fix/interface-destination-v2 branch November 8, 2026 05:39
@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