Skip to content

fix: empty slice panics in bead_sort, bogo_sort, comb_sort and wave_sort - #1063

Open
SEPURI-SAI-KRISHNA wants to merge 1 commit into
TheAlgorithms:masterfrom
SEPURI-SAI-KRISHNA:fix/sorting-empty-slice-panics
Open

fix: empty slice panics in bead_sort, bogo_sort, comb_sort and wave_sort#1063
SEPURI-SAI-KRISHNA wants to merge 1 commit into
TheAlgorithms:masterfrom
SEPURI-SAI-KRISHNA:fix/sorting-empty-slice-panics

Conversation

@SEPURI-SAI-KRISHNA

Copy link
Copy Markdown

Pull Request Template

Description

Four algorithms in src/sorting/ panic when handed an empty slice. Sorting nothing should
be a no-op, and the other 30 sorts in the module already treat it that way — 20 of them have
an explicit empty() test. These four were simply never exercised on that input.

sorting::bead_sort(&mut []);                 // index out of bounds: the len is 0 but the index is 0
sorting::bogo_sort(&mut [] as &mut [i32]);   // attempt to subtract with overflow
sorting::comb_sort(&mut [] as &mut [i32]);   // attempt to subtract with overflow
sorting::wave_sort(&mut [] as &mut [i32]);   // attempt to subtract with overflow

Each is an unsigned underflow or an unchecked first-element read:

  • bead_sortlet mut max = a[0]; reads element 0 before checking the slice is
    non-empty. Now returns early; there is no bead grid to build.
  • bogo_sort — the private is_sorted helper loops for i in 0..len - 1, which
    underflows to usize::MAX at len == 0. Rewritten with windows(2), which naturally
    yields nothing below two elements. This also drops a hand-rolled loop in favour of the
    same idiom sorting::is_sorted already uses.
  • comb_sortgap is clamped to a minimum of 1, so for i in 0..arr.len() - gap
    underflows at arr.len() == 0. Now returns early for fewer than two elements.
  • wave_sortfor i in (0..n - 1).step_by(2) underflows at n == 0. Now uses
    n.saturating_sub(1).

These are release-mode hazards as well as debug-mode panics: with overflow checks off, the
underflowed bound becomes a near-usize::MAX loop bound and the sort runs off the end of
the slice.

Tests

empty and one_element cases added to all four, following the module's existing
convention of asserting with the shared is_sorted / have_same_elements helpers.
bead_sort also gains an all_zeroes case, since a maximum of 0 is the other way its
bead grid can end up with zero columns. All new tests run in well under 300ms.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • I ran bellow commands using the latest version of rust nightly.
  • I ran cargo clippy --all -- -D warnings just before my last commit and fixed any issue that was found.
  • I ran cargo fmt just before my last commit.
  • I ran cargo test just before my last commit and all tests passed.
  • I added my algorithm to the corresponding mod.rs file within its own folder, and in any parent folder(s).
  • I added my algorithm to DIRECTORY.md with the correct link.
  • I checked COUNTRIBUTING.md and my code follows its guidelines.

Note: no mod.rs or DIRECTORY.md change was needed — this fixes existing entries rather
than adding a new algorithm.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.90%. Comparing base (2c53ddf) to head (0f1909e).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1063      +/-   ##
==========================================
+ Coverage   95.89%   95.90%   +0.01%     
==========================================
  Files         396      396              
  Lines       30440    30492      +52     
==========================================
+ Hits        29190    29243      +53     
+ Misses       1250     1249       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

2 participants