Skip to content

Fix EqualExportedValues on recursive values#1916

Open
cyphercodes wants to merge 1 commit into
stretchr:masterfrom
cyphercodes:fix-equal-exported-recursive
Open

Fix EqualExportedValues on recursive values#1916
cyphercodes wants to merge 1 commit into
stretchr:masterfrom
cyphercodes:fix-equal-exported-recursive

Conversation

@cyphercodes

Copy link
Copy Markdown

Summary

Fix EqualExportedValues so copying exported fields terminates for recursive values.

Changes

  • Track visited pointer/slice/map values while copying exported fields.
  • Reuse in-progress copies when a recursive value is encountered.
  • Add a regression test for self-referential struct pointers with differing unexported fields.

Motivation

copyExportedFields recursed indefinitely on pointer cycles, causing EqualExportedValues to time out or overflow the stack for recursive data structures.

Exact test commands run:

  • go test ./assert -run TestEqualExportedValuesRecursiveStruct -count=1 -timeout=3s (failed before the fix; timed out reproducing the bug)
  • go test ./assert -run 'TestEqualExportedValuesRecursiveStruct|TestEqualExportedValues|TestObjectsExportedFieldsAreEqual|TestCopyExportedFields' -count=1
  • go test ./assert -count=1
  • go test ./... -count=1
  • git diff --check

Related issues

Fixes #1915

Comment thread assert/assertions_test.go
if !EqualExportedValues(t, expected, actual) {
t.Error("expected EqualExportedValues to handle recursive values")
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add tests to cover reflect.Map and reflect.Array/Slice? I think they are the hairiest parts of this PR and currently uncovered.

I wonder if it's possible to move the result, ok := visited[visit]; ok check earlier in the function to avoid spreading it out to 3 different places. Maybe it's not possible?

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.

EqualExportedValues overflow the stack on recursive data structures

2 participants