Skip to content

fix(mappings): don't panic when deleting a reference from a mapping without references - #4282

Open
ch33hau wants to merge 1 commit into
loft-sh:mainfrom
ch33hau:fix-deletereference-empty-references
Open

ch33hau wants to merge 1 commit into
loft-sh:mainfrom
ch33hau:fix-deletereference-empty-references

Conversation

@ch33hau

@ch33hau ch33hau commented Nov 14, 2026 •

Copy link
Copy Markdown

Store.DeleteReference allocated its new slice with capacity len(mapping.References)-1, which is -1 (makeslice: cap out of range, see the added test) when the owner mapping has no references yet.
This PR changes it to use len(mapping.References).

What issue type does this pull request address? (keep at least one, remove the others)
/kind bugfix

What does this pull request do? Which issues does it resolve? (use resolves #<issue_number> if possible)
No related issue.

Please provide a short message that should be published in the vcluster release notes
Fixed a potential panic in the mappings store when deleting a reference from a mapping that has no references.

What else do we need to know?
This is just a robustness fix. I can't see how a user could trigger the panic code, but DeleteReference is part of the public interface in synccontext.Mapper, plus the fix is simple, so maybe worth to fix it.

The fix allocates one extra slot of capacity. I assume it is ok. It could also be max(len(mapping.References)-1, 0) if a more optimized code is preferred.


Note

Low Risk
Small robustness fix in reference bookkeeping with no change to happy-path behavior; only avoids a panic on an edge case.

Overview
Fixes a panic in Store.DeleteReference when the owner mapping exists but has an empty References slice. The slice was preallocated with capacity len(mapping.References)-1, which is -1 in that case and triggers makeslice: cap out of range.

The allocation now uses len(mapping.References) (capacity 0 when there are no references), so deleting a reference that was never added remains a no-op as intended.

Adds TestDeleteReferenceWithoutReferences to lock in that behavior and guard against regressions.

Reviewed by Cursor Bugbot for commit f25ad09. Bugbot is set up for automated code reviews on this repo. Configure here.

…ithout references

Store.DeleteReference allocated its new slice with capacity
len(mapping.References)-1, which is -1 (makeslice: cap out of range) when
the owner mapping has no references yet. Use len(mapping.References).

This branch has not been deployed

No deployments
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