fix(assertions): guard reflection walkers against cyclic inputs#129
Merged
Merged
Conversation
Three hand-rolled reflection traversals recursed unconditionally and would
overflow the goroutine stack (a fatal, unrecoverable runtime error) when given
cyclic-but-legal Go values:
- copyExportedFields (reached via EqualExportedValues): a self-referential
struct such as `type N struct{ Next *N }` with `n.Next = n` recursed forever.
- isEmptyValue (Empty/NotEmpty/Zero/NotZero): a cyclic pointer chain
(`type P *P` with `p = &p`) recursed forever.
- unwrapAll (error-chain formatting): an error with a cyclic Unwrap chain
recursed forever.
Each walker now carries a set of values currently being visited on the
recursion path and breaks back-edges, while leaving non-cyclic (including
shared/diamond) inputs unchanged. unwrapAll only tracks comparable errors, since
using an incomparable error as a map key would panic (mirroring how the standard
library's errors.Is guards its own comparisons).
copyExportedFields is split into per-kind helpers to keep cognitive complexity
within the linter threshold.
Adds recursion_cycles_test.go with regression tests for each case. The signal
is simply that the tests run to completion: before the fix each case crashes the
test binary with a stack overflow.
Note: the public ErrorIs/NotErrorIs path on a cyclic error chain still hangs
inside the standard library's errors.Is (which walks Unwrap without cycle
detection) before our formatter runs. Guarding that would mean reimplementing
errors.Is and diverging from stdlib semantics, so it is left as-is; unwrapAll is
hardened on its own as defense-in-depth.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Frédéric BIDON <fredbi@yahoo.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #129 +/- ##
==========================================
+ Coverage 91.68% 91.70% +0.01%
==========================================
Files 97 97
Lines 12623 12651 +28
==========================================
+ Hits 11573 11601 +28
Misses 827 827
Partials 223 223 ☔ View full report in Codecov by Harness. |
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.
Three hand-rolled reflection traversals recursed unconditionally and would overflow the goroutine stack (a fatal, unrecoverable runtime error) when given cyclic-but-legal Go values:
type N struct{ Next *N }withn.Next = nrecursed forever.type P *Pwithp = &p) recursed forever.Each walker now carries a set of values currently being visited on the recursion path and breaks back-edges, while leaving non-cyclic (including shared/diamond) inputs unchanged. unwrapAll only tracks comparable errors, since using an incomparable error as a map key would panic (mirroring how the standard library's errors.Is guards its own comparisons).
copyExportedFields is split into per-kind helpers to keep cognitive complexity within the linter threshold.
Adds recursion_cycles_test.go with regression tests for each case. The signal is simply that the tests run to completion: before the fix each case crashes the test binary with a stack overflow.
Note: the public ErrorIs/NotErrorIs path on a cyclic error chain still hangs inside the standard library's errors.Is (which walks Unwrap without cycle detection) before our formatter runs. Guarding that would mean reimplementing errors.Is and diverging from stdlib semantics, so it is left as-is; unwrapAll is hardened on its own as defense-in-depth.
Change type
Please select: 🆕 New feature or enhancement|🔧 Bug fix'|📃 Documentation update
Short description
Fixes
Full description
Checklist