From c5984402b8e74e6f85628946dbb009c5aaa5328c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Ravier?= Date: Mon, 9 Oct 2023 18:44:56 +0200 Subject: [PATCH] chunking: Handle low amount of high-size packages If we have a low amount of high-size packages then we will drain with an index higher than the size of the Vec which triggers a panic. Fixes: https://github.com/coreos/rpm-ostree/issues/4646 --- lib/src/chunking.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/src/chunking.rs b/lib/src/chunking.rs index d7af2f16..3e424d71 100644 --- a/lib/src/chunking.rs +++ b/lib/src/chunking.rs @@ -462,8 +462,12 @@ fn get_partitions_with_threshold<'a>( } // Extra high-size packages - let mut remaining_pkgs: Vec<_> = high_size.drain(limit_hs_bins..).collect(); - assert_eq!(high_size.len(), limit_hs_bins); + let mut remaining_pkgs: Vec<_> = if high_size.len() < limit_hs_bins { + Vec::new() + } else { + high_size.drain(limit_hs_bins..).collect() + } + assert_lt!(high_size.len(), limit_hs_bins); // Concatenate extra high-size packages + med_sizes to keep it descending sorted remaining_pkgs.append(&mut med_size);