PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
Never-crash gate on the oracle and testkit crates; oracle find() no longer panics on an empty needle (#3435)
Giuseppe Silanob11389646132
diff --git a/crates/oracle/src/lib.rs b/crates/oracle/src/lib.rsindex e9e7ae372..16c7192b1 100644--- a/crates/oracle/src/lib.rs+++ b/crates/oracle/src/lib.rs@@ -23,6 +23,8 @@//! This crate is test scaffolding (`Class::Testkit` in `xtask/src/layers.rs`): a dev-dependency//! only, never compiled into the product.+#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]+pub mod pixels;use std::fmt;diff --git a/crates/oracle/src/pixels.rs b/crates/oracle/src/pixels.rsindex 2b1171981..4ff0ebaf1 100644--- a/crates/oracle/src/pixels.rs+++ b/crates/oracle/src/pixels.rs@@ -201,6 +201,10 @@ pub fn decode_pam_gray(bytes: &[u8]) -> Option<(usize, usize, Vec<u8>)> {}fn find(haystack: &[u8], needle: &[u8]) -> Option<usize> {+ // `windows(0)` panics.+ if needle.is_empty() {+ return None;+ }haystack.windows(needle.len()).position(|w| w == needle)}@@ -404,6 +408,14 @@ mod tests {assert_eq!((w, h, px.len()), (4, 2, 8));}+ /// An empty needle is not found, rather than panicking in `windows(0)`.+ #[test]+ fn find_with_an_empty_needle_is_none() {+ assert_eq!(find(b"abc", b""), None);+ assert_eq!(find(b"", b""), None);+ assert_eq!(find(b"abcabc", b"ca"), Some(2));+ }+/// Malformed or hostile headers return `None` rather than panicking or inventing a size.#[test]fn hostile_pgm_headers_are_rejected() {diff --git a/crates/testkit/src/lib.rs b/crates/testkit/src/lib.rsindex c9f5e55ef..b4f69a280 100644--- a/crates/testkit/src/lib.rs+++ b/crates/testkit/src/lib.rs@@ -25,6 +25,8 @@//! This crate is test scaffolding (`Class::Testkit` in `xtask/src/layers.rs`): a dev-dependency//! only, never compiled into the product.+#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]+/// A small PDF with a classic cross-reference table, built one object at a time.////// Objects are numbered from 1 in the order they are added, all at generation 0, so the first