From e78a079430bbc9fe551d55df515ac3f51135cef4 Mon Sep 17 00:00:00 2001 From: Amey Pawar <138877912+ameyypawar@users.noreply.github.com> Date: Tue, 30 Jun 2026 17:57:45 +0530 Subject: [PATCH] fix(vchordg): avoid panic when validating an empty alpha option `validate_alpha` indexes `alpha[0]` after the sorted/range checks, both of which pass on an empty slice (`[].is_sorted()` and `[].iter().all(..)` are true). So an empty `alpha` (e.g. `WITH (options='[index] alpha = []')`) panics with an index-out-of-bounds instead of returning a ValidationError. Use `alpha.first()` so the empty case yields the existing "`alpha` should contain `1.0`" error. Behavior is unchanged for every non-empty input. Add unit tests covering the empty slice plus valid/invalid cases. --- crates/vchordg/src/types.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/crates/vchordg/src/types.rs b/crates/vchordg/src/types.rs index 62ec3cc4..5a5a6169 100644 --- a/crates/vchordg/src/types.rs +++ b/crates/vchordg/src/types.rs @@ -56,7 +56,7 @@ impl VchordgIndexOptions { if !alpha.iter().all(|x| (1.0..2.0).contains(x)) { return Err(ValidationError::new("alpha is too large or too small")); } - if alpha[0] != 1.0 { + if alpha.first() != Some(&1.0) { return Err(ValidationError::new("`alpha` should contain `1.0`")); } Ok(()) @@ -144,3 +144,25 @@ impl Structure { self.children.is_empty() } } + +#[cfg(test)] +mod tests { + use super::VchordgIndexOptions; + + #[test] + fn validate_alpha_handles_empty_without_panicking() { + let empty: &[f32] = &[]; + assert!(VchordgIndexOptions::validate_alpha(empty).is_err()); + } + + #[test] + fn validate_alpha_accepts_default() { + assert!(VchordgIndexOptions::validate_alpha(&[1.0, 1.2]).is_ok()); + } + + #[test] + fn validate_alpha_rejects_unsorted_or_out_of_range() { + assert!(VchordgIndexOptions::validate_alpha(&[1.2, 1.0]).is_err()); + assert!(VchordgIndexOptions::validate_alpha(&[2.0]).is_err()); + } +}