Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

Reject '+' sign in page selection numbers (#5715)

NetterSilverd7dd1de647ee
diff --git a/crates/ui-egui/src/panels/page_selection_ui.rs b/crates/ui-egui/src/panels/page_selection_ui.rs
index eb97b16b0..deb811768 100644
--- a/crates/ui-egui/src/panels/page_selection_ui.rs
+++ b/crates/ui-egui/src/panels/page_selection_ui.rs
@@ -22,7 +22,9 @@ pub(crate) fn parse(text: &str, count: usize) -> Result<Vec<usize>, PrintError>
let mut pages = BTreeSet::new();
let mut budget = 1_000_000usize;
let number = |token: &str| {
- let n = token.trim().parse::<usize>().map_err(|_| PrintError::NotAPage(token.into()))?;
+ let t = token.trim();
+ let n =
+ if t.bytes().all(|b| b.is_ascii_digit()) { t.parse::<usize>().ok() } else { None }.ok_or_else(|| PrintError::NotAPage(token.into()))?;
if n == 0 || n > count { Err(PrintError::OutOfRange(n, count)) } else { Ok(n - 1) }
};
for part in text.split([',', ';']).map(str::trim) {
@@ -132,6 +134,13 @@ impl PageSelectionInput {
mod tests {
use super::*;
+ #[test]
+ fn plus_sign_is_not_a_page_number() {
+ for text in ["+3", "+2-4", "2-+4"] {
+ assert!(matches!(parse(text, 100), Err(PrintError::NotAPage(_))), "{text}");
+ }
+ }
+
#[test]
fn selection_ranges_are_sorted_unique_and_bounded() {
let pages = parse("1,12,16-18,89,92,93,97,99", 100).unwrap();