J-F-Liu / lopdf

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

Panic `called Result::unwrap() on an Err value: ReferenceCycle` in `src/document.rs:446:83` #303

Closed qarmin closed 3 months ago

qarmin commented 3 months 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)
        }
    }
}

panic

thread 'main' panicked at /home/runner/.cargo/git/checkouts/lopdf-0013854ec52a6141/c25cef3/src/document.rs:446:83:
called `Result::unwrap()` on an `Err` value: ReferenceCycle
stack backtrace:
   0: rust_begin_unwind
             at /rustc/91376f416222a238227c84a848d168835ede2cc3/library/std/src/panicking.rs:662:5
   1: core::panicking::panic_fmt
             at /rustc/91376f416222a238227c84a848d168835ede2cc3/library/core/src/panicking.rs:74:14
   2: core::result::unwrap_failed
             at /rustc/91376f416222a238227c84a848d168835ede2cc3/library/core/src/result.rs:1679:5
   3: core::result::Result<T,E>::unwrap
             at /rustc/91376f416222a238227c84a848d168835ede2cc3/library/core/src/result.rs:1102:23
   4: lopdf::document::Document::get_page_resources
             at /home/runner/.cargo/git/checkouts/lopdf-0013854ec52a6141/c25cef3/src/document.rs:446:13
   5: lopdf::document::Document::get_page_fonts
             at /home/runner/.cargo/git/checkouts/lopdf-0013854ec52a6141/c25cef3/src/document.rs:478:45
   6: lopdf::parser_aux::<impl lopdf::document::Document>::extract_text
             at /home/runner/.cargo/git/checkouts/lopdf-0013854ec52a6141/c25cef3/src/parser_aux.rs:71:25
   7: lopdf::check_file
             at ./src/crates/lopdf/src/main.rs:41:29
   8: lopdf::main
             at ./src/crates/lopdf/src/main.rs:23:9
   9: core::ops::function::FnOnce::call_once
             at /rustc/91376f416222a238227c84a848d168835ede2cc3/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)"