fix: avoid append aliasing of caller-owned component slices#129
Merged
Conversation
Contributor
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Enterprise Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
WalkthroughIn two places, the code now preallocates a fresh local slice before appending child components and the root component, instead of appending the root directly onto the existing child slice. ChangesSlice aliasing fix
Estimated code review effort: 2 (Simple) | ~5 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
Isan-Rivkin
previously approved these changes
Jul 1, 2026
NewComponentFactory and KartaValidator.initialize assembled a combined "children + root" list with append(ChildComponents, RootComponent). When the caller's ChildComponents slice has spare capacity (cap > len), append writes RootComponent into the caller-owned backing array rather than allocating a fresh one, mutating memory the caller still owns. Allocate a dedicated slice sized len(ChildComponents)+1 and copy into it, so the caller's slice is never touched. Signed-off-by: Ron Lev <rlev@nvidia.com>
f1b55be to
f6b1524
Compare
Isan-Rivkin
approved these changes
Jul 1, 2026
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.
What does this PR do?
Fixes an append-aliasing footgun in two spots that assemble a combined "children + root" component list:
pkg/resource/component_factory.go(NewComponentFactory)pkg/api/runai/v1alpha1/validation.go(KartaValidator.initialize)Both did:
append(s, x)reusess's backing array whenevercap(s) > len(s).ChildComponentsis owned by the caller'sKarta, so when that slice has spare capacity the append writesRootComponentinto caller-owned memory instead of allocating fresh. A freshly unmarshalledKarta(JSON/YAML decoders over-allocate slice capacity) can hit this; a DeepCopy (make([]T, len(...))) cannot.The fix allocates a dedicated slice sized
len(ChildComponents)+1and copies into it, so the caller's slice is never touched. Iteration contents are unchanged.Related issue(s)
N/A
Checklist
git commit -s)make check)Summary by CodeRabbit