Repository: J-F-Liu/lopdf (language: rust)

Issue:
Panic `attempt to add with overflow` in `src/reader.rs`
```
thread 'main' panicked at /home/rafal/.cargo/git/checkouts/lopdf-0013854ec52a6141/c25cef3/src/reader.rs:256:32:
attempt to add with overflow
stack backtrace:
   0: rust_begin_unwind
             at /rustc/7120fdac7a6e55a5e4b606256042890b36067052/library/std/src/panicking.rs:665:5
   1: core::panicking::panic_fmt
             at /rustc/7120fdac7a6e55a5e4b606256042890b36067052/library/core/src/panicking.rs:74:14
   2: core::panicking::panic_const::panic_const_add_overflow
             at /rustc/7120fdac7a6e55a5e4b606256042890b36067052/library/core/src/panicking.rs:181:21
   3: lopdf::reader::Reader::read
             at /home/rafal/.cargo/git/checkouts/lopdf-0013854ec52a6141/c25cef3/src/reader.rs:256:32
   4: lopdf::reader::<impl lopdf::document::Document>::load_internal
             at /home/rafal/.cargo/git/checkouts/lopdf-0013854ec52a6141/c25cef3/src/reader.rs:60:9
   5: lopdf::reader::<impl lopdf::document::Document>::load_from
             at /home/rafal/.cargo/git/checkouts/lopdf-0013854ec52a6141/c25cef3/src/reader.rs:51:9
   6: lopdf::check_file
             at /home/rafal/Projekty/Rust/run_command_for_every_file/src/crates/lopdf/src/main.rs:32:11
   7: lopdf::main
             at /home/rafal/Projekty/Rust/run_command_for_every_file/src/crates/lopdf/src/main.rs:23:9
   8: core::ops::function::FnOnce::call_once
             at /rustc/7120fdac7a6e55a5e4b606256042890b36067052/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
Aborted (core dumped)

```
[crash-edd7e3c13df9c9e7dc91058d7bda1fa6fca4a931.zip](https://github.com/user-attachments/files/16632955/crash-edd7e3c13df9c9e7dc91058d7bda1fa6fca4a931.zip)


```
fn check_file(file_path: &str) {
    let Ok(content) = fs::read(file_path) else {
        return;
    };
    let cursor = Cursor::new(content);
    match Document::load_from(cursor) {
        Ok(mut document) => {
            let pages = document.get_pages();

            let mut doc_clone = document.clone();
            doc_clone.decompress();

            for (i, _) in pages.iter().enumerate() {
                let page_number = (i + 1) as u32;
                let _text = document.extract_text(&[page_number]);
            }

            let _ = document.save_to(&mut Cursor::new(Vec::new()));
        }
        Err(err) => {
            eprintln!("Error reading PDF contents: {}", err)
        }
    }
}

```


Discussion / hints:

Fix patch:
diff --git a/src/reader.rs b/src/reader.rs
index eb16d0d..2b2a156 100644
--- a/src/reader.rs
+++ b/src/reader.rs
@@ -260,8 +260,7 @@ impl<'a> Reader<'a> {
 
             prev_xref_start = prev_trailer.get(b"Prev").cloned();
         }
-
-        let xref_entry_count = xref.max_id() + 1;
+        let xref_entry_count = xref.max_id().checked_add(1).ok_or(Error::Xref(XrefError::Parse))?;
         if xref.size != xref_entry_count {
             warn!(
                 "Size entry of trailer dictionary is {}, correct value is {}.",


Test patch: