J-F-Liu / lopdf

A Rust library for PDF document manipulation.
MIT License
1.67k stars 176 forks source link

Panic `attempt to subtract with overflow` in `src/object.rs` #353

Closed qarmin closed 1 week ago

qarmin commented 1 week ago

code

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)
        }
    }
}

cause this

thread 'main' panicked at /home/runner/.cargo/git/checkouts/lopdf-0013854ec52a6141/2d6052f/src/object.rs:712:34:
attempt to subtract with overflow
stack backtrace:
   0: rust_begin_unwind
             at /rustc/5ec7d6eee7e0f5236ec1559499070eaf836bc608/library/std/src/panicking.rs:665:5
   1: core::panicking::panic_fmt
             at /rustc/5ec7d6eee7e0f5236ec1559499070eaf836bc608/library/core/src/panicking.rs:76:14
   2: core::panicking::panic_const::panic_const_sub_overflow
             at /rustc/5ec7d6eee7e0f5236ec1559499070eaf836bc608/library/core/src/panicking.rs:182:21
   3: lopdf::object::Stream::decode_ascii85
             at /home/runner/.cargo/git/checkouts/lopdf-0013854ec52a6141/2d6052f/src/object.rs:712:34
   4: lopdf::object::Stream::decompressed_content
             at /home/runner/.cargo/git/checkouts/lopdf-0013854ec52a6141/2d6052f/src/object.rs:652:36
   5: lopdf::object::Stream::decompress
             at /home/runner/.cargo/git/checkouts/lopdf-0013854ec52a6141/2d6052f/src/object.rs:773:20
   6: lopdf::processor::<impl lopdf::document::Document>::decompress
             at /home/runner/.cargo/git/checkouts/lopdf-0013854ec52a6141/2d6052f/src/processor.rs:37:25
   7: lopdf::check_file
             at ./src/crates/lopdf/src/main.rs:37:13
   8: lopdf::main
             at ./src/crates/lopdf/src/main.rs:23:9
   9: core::ops::function::FnOnce::call_once
             at /home/runner/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

##### Automatic Fuzzer note, output status "None", output signal "Some(6)"

compressed.zip