fix: Gff.AddFeature deep copies Attributes and Location.SubLocations - #451
Open
techreign wants to merge 1 commit into
Open
fix: Gff.AddFeature deep copies Attributes and Location.SubLocations#451techreign wants to merge 1 commit into
techreign wants to merge 1 commit into
Conversation
AddFeature intended to store a copy of the given Feature, but only dereferenced the pointer (featureCopy := *feature), which is a shallow copy. Feature.Attributes (a map) and Feature.Location.SubLocations (a slice, recursively) were still shared between the caller's Feature and the one appended to Gff.Features, so mutating one after the call leaked into the other. AddFeature now deep copies the Attributes map and recursively deep copies Location's SubLocations, and its doc comment explains why. Added TestAddFeature_doesNotShareState to prove mutations no longer leak either direction, and a CHANGELOG entry. Fixes bebop#342 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
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.
Changes in this PR
Gff.AddFeature()now deep copies theFeature.Attributesmap and recursively deep copiesFeature.Location.SubLocationsbefore appending the feature toGff.Features, instead of only dereferencing the*Featurepointer (which is a shallow copy of the struct and still shares the map/slice with the caller).Location.deepCopy()helper used to recursively copy nestedSubLocations.AddFeature's doc comment to explain what it actually does and why the deep copy is necessary.TestAddFeature_doesNotShareStateinio/gff/gff_test.go, which proves that mutating the caller'sAttributes/SubLocationsafterAddFeaturedoes not affect the stored feature, and that mutating the stored feature does not affect the caller's copy.CHANGELOG.mdentry under[Unreleased] / Fixed.Feature.ParentSequenceis still set on the caller's original struct (as before) sofeature.GetSequence()keeps working directly on the struct passed intoAddFeature- this preserves the existing behavior relied on byExampleGff_AddFeature/ExampleFeature_GetSequence. The issue also floated droppingParentSequenceentirely and removingAddFeature, but that's a breaking API change; this PR sticks to the minimal, non-breaking fix for the actual mutation bug reported.Why are you making these changes?
Gff.AddFeature()was documented/intended to store a copy of the givenFeature, butfeatureCopy := *featureonly shallow-copies the struct. Reference-typed fields (theAttributesmap, and theLocation.SubLocationsslice) kept pointing at the same underlying map/slice as the caller'sFeature, so mutating either the caller's originalFeatureor the stored one after the call would silently leak into the other.Fixes: #342
Are any changes breaking? (IMPORTANT)
No.
AddFeature's signature and exported behavior are unchanged; only the aliasing bug is fixed. No new exported identifiers besides behavior already documented (the newdeepCopymethod is unexported).Pre-merge checklist
AddFeature's docstring was updated.)TestAddFeature_doesNotShareState)ExampleGff_AddFeature/ExampleFeature_GetSequencealready coverAddFeatureusage and still pass.)CHANGELOG.mdin the[Unreleased]section.Verification
Note:
TestGffIO,Example_basic,ExampleRead,ExampleParse, andExampleBuild(all of which parsedata/ecoli-mg1655-short.gfffrom disk) fail on my Windows dev environment becausecore.autocrlf=truechecks the.gfffixture out with CRLF line endings, which the hand-rolled line/field splitting inParsedoesn't expect. This reproduces identically on a clean checkout ofmainwith no changes applied (verified viagit stash), so it is a pre-existing, environment-specific issue unrelated to this fix, and out of scope here.golangci-lintwas not available in this environment, so it was not run.