cp: prevent panic in aligned_ancestors on shallow source paths#13275
Open
AnuthaDev wants to merge 1 commit into
Open
cp: prevent panic in aligned_ancestors on shallow source paths#13275AnuthaDev wants to merge 1 commit into
AnuthaDev wants to merge 1 commit into
Conversation
|
GNU testsuite comparison: |
Merging this PR will not alter performance
Comparing Footnotes
|
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.
Problem
The
aligned_ancestorsfunction insrc/uu/cp/src/cp.rspanicked when called with an empty source path, which has only 1 ancestor.The root cause was unsafe slice indexing:
When
n == 1(empty source""), the range[1..0]has start > end and panics.Reproduction
Fix
Rewrote
aligned_ancestorsto use iterator combinators instead of manual slice indexing.Also reduced allocations from 3
Vecs (two ancestor collects + result) to 1 (the zip collect).Tests
Added 3 unit tests in
src/uu/cp/src/cp.rs:test_aligned_ancestors_empty_source— empty source path (regression: previously panicked)test_aligned_ancestors_single_component_source— single-component source (boundary: no intermediates)test_aligned_ancestors_two_component_source— two-component source (boundary: 1 intermediate)Added 1 integration test in
tests/by-util/test_cp.rs:test_cp_parents_empty_source_does_not_panic— verifiescp --parents "" destfails gracefully with a "cannot stat" error instead of panicking