assert: detect cycles in EqualExportedValues - #1946
Closed
22elix3r wants to merge 1 commit into
Closed
Conversation
copyExportedFields followed pointer, slice, and map cycles until the stack overflowed. Track visited values the same way encoding/json does and reuse in-progress copies so cyclic equal structures compare equal. Failure messages use spew, which already handles cycles, instead of fmt %#v so unequal cyclic values do not overflow when formatting. Fixes stretchr#1915 Signed-off-by: elix3r <157088510+22elix3r@users.noreply.github.com>
Author
|
Ready for review whenever a maintainer is available. I cannot request a reviewer from this account. |
Author
|
Withdrawing: I missed that this issue already had earlier open PRs. Sorry for the noise — leaving the field to those patches. |
Author
|
Withdrawing this PR so the earlier open work on the issue can proceed. |
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.
Summary
EqualExportedValuescopies exported fields viacopyExportedFields, which recursed forever on pointer, slice, and map cycles and aborted the test binary withfatal error: stack overflow. This change detects those cycles (same identity keys asencoding/json) and reuses the in-progress copy so cyclic equal structures compare equal, matchingreflect.DeepEqual.This is a bug fix. Example (the report from #1915):
Cyclic structures that differ in exported fields still compare unequal. Cyclic vs non-cyclic values (self-pointer vs nil) also remain unequal, because the copy preserves the cycle instead of zeroing it.
Related Issue
Fixes #1915
Changes Made
copyExportedFieldsusing a visit key in the same shape asencoding/json(type + pointer, plus length for slices).EqualExportedValuesfailure messages with spew (which already handles cycles and is used for diffs) instead offmt%#v, so unequal cyclic values do not overflow while printing.Testing
gofmtcleango vet ./assertgo test ./assert -count=1 -timeout=30s -run 'TestCopyExportedFields|TestEqualExportedValues|TestObjectsExportedFieldsAreEqual'go test ./assert -count=1go test -race ./assert -count=1go test ./... -count=1Notes
reflect.DeepEqualcan tell those cases apart.fmt%#vdoes not handle cycles). Happy to close this if Fix EqualExportedValues on recursive values #1916 is updated instead.