Skip to content

fix(assert): prevent stack overflow on cyclic data structures in EqualExportedValues - #1939

Open
AruneshDwivedi wants to merge 1 commit into
stretchr:masterfrom
AruneshDwivedi:fix/cyclic-struct-equal
Open

fix(assert): prevent stack overflow on cyclic data structures in EqualExportedValues#1939
AruneshDwivedi wants to merge 1 commit into
stretchr:masterfrom
AruneshDwivedi:fix/cyclic-struct-equal

Conversation

@AruneshDwivedi

Copy link
Copy Markdown

Problem

assert.EqualExportedValues panics with a stack overflow when comparing structs that contain self-referential pointers (cyclic data structures). The copyExportedFields function recurses infinitely following the cycle until the goroutine stack is exhausted.

Example

type Node struct {
    Self *Node
}

a := &Node{}
a.Self = a
b := &Node{}
b.Self = b
assert.EqualExportedValues(t, a, b) // PANIC: stack overflow

Fix

Added cycle detection to copyExportedFields by tracking visited pointers in a map[uintptr]struct{}. When a pointer is revisited, the function returns nil instead of recursing infinitely.

Changes

  • assert/assertions.go: Modified copyExportedFields to accept a seen map for cycle detection
  • assert/assertions_test.go: Updated test to use new helper function

Testing

All existing tests pass. The fix prevents stack overflow while maintaining correct behavior for non-cyclic structures.

…lExportedValues

When comparing struct values with self-referential pointers using
EqualExportedValues, copyExportedFields would recurse infinitely
causing a stack overflow.

Fixed by tracking visited pointers in a seen map and breaking
cycles by returning nil for already-seen pointers.
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.

1 participant