From 62978f65f295a09dc8bfde14883662deb531cf92 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Mon, 28 Aug 2023 13:49:54 -0400 Subject: [PATCH] chunking: Add const for minimum, change to regular error Avoid a panic in this case as it can be reachable via external input. xref https://github.com/coreos/rpm-ostree/issues/4530 --- lib/src/chunking.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/src/chunking.rs b/lib/src/chunking.rs index 094b50dc..cef60b2f 100644 --- a/lib/src/chunking.rs +++ b/lib/src/chunking.rs @@ -26,6 +26,9 @@ use serde::{Deserialize, Serialize}; // We take half the limit of 128. // https://github.com/ostreedev/ostree-rs-ext/issues/69 pub(crate) const MAX_CHUNKS: u32 = 64; +/// Minimum number of layers we can create in a "chunked" flow; otherwise +/// we will just drop down to one. +const MIN_CHUNKED_LAYERS: u32 = 4; type RcStr = Rc; pub(crate) type ChunkMapping = BTreeMap)>; @@ -638,6 +641,8 @@ fn basic_packing<'a>( const HIGH_SIZE_CUTOFF: f32 = 0.6; let before_processing_pkgs_len = components.len(); + anyhow::ensure!(bin_size.get() >= MIN_CHUNKED_LAYERS); + // If we have a prior build, then use that if let Some(prior_build) = prior_build_metadata { return basic_packing_with_prior_build(components, bin_size, prior_build); @@ -687,7 +692,7 @@ fn basic_packing<'a>( // Approximate number of components we should have per medium-size bin. let pkg_per_bin_ms: usize = (components.len() - limit_hs_bins - low_sized_component_count) .checked_div(limit_ms_bins) - .expect("number of bins should be >= 4"); + .ok_or_else(|| anyhow::anyhow!("number of bins should be >= {}", MIN_CHUNKED_LAYERS))?; // Bins assignment for (partition, pkgs) in partitions.iter() { @@ -795,6 +800,15 @@ mod test { Ok(()) } + #[test] + fn test_packing_one_layer() -> Result<()> { + let contentmeta: Vec = + serde_json::from_reader(flate2::read::GzDecoder::new(FCOS_CONTENTMETA))?; + let r = basic_packing(&contentmeta, NonZeroU32::new(1).unwrap(), None); + assert!(r.is_err()); + Ok(()) + } + fn create_manifest(prev_expected_structure: Vec>) -> oci_spec::image::ImageManifest { let mut p = prev_expected_structure .iter()