From ab3555f5a7fa6347049159ee3a32730f9a0c637c Mon Sep 17 00:00:00 2001 From: Yureka Date: Tue, 1 Oct 2024 16:16:54 +0200 Subject: fix(tvix/castore/refscan): don't panic on empty patterns Previously, the overlap calculation would underflow when the pattern is empty. Change-Id: I1f6bf49fafc4b8183a3a5e5e491a5a5bfc41ca97 Reviewed-on: https://cl.tvl.fyi/c/depot/+/12558 Reviewed-by: flokli Tested-by: BuildkiteCI Autosubmit: yuka --- tvix/castore/src/refscan.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'tvix/castore/src/refscan.rs') diff --git a/tvix/castore/src/refscan.rs b/tvix/castore/src/refscan.rs index 0b8af296bb50..8e6a81b2a4fe 100644 --- a/tvix/castore/src/refscan.rs +++ b/tvix/castore/src/refscan.rs @@ -192,7 +192,15 @@ where self: Pin<&mut Self>, cx: &mut std::task::Context<'_>, ) -> Poll> { - let overlap = self.scanner.pattern.longest_candidate() - 1; + #[allow(clippy::manual_saturating_arithmetic)] // for clarity + let overlap = self + .scanner + .pattern + .longest_candidate() + .checked_sub(1) + // If this overflows (longest_candidate = 0), that means there are no needles, + // so there is no need to have any overlap + .unwrap_or(0); let mut this = self.project(); // Still data in buffer if *this.consumed < this.buffer.len() { -- cgit 1.4.1