Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic in setup_outline_page_ids() #263

Closed
asdfuser opened this issue Feb 18, 2024 · 3 comments · Fixed by #351
Closed

Panic in setup_outline_page_ids() #263

asdfuser opened this issue Feb 18, 2024 · 3 comments · Fixed by #351

Comments

@asdfuser
Copy link

Calling get_toc() on the following pdf causes a panic in setup_outline_page_ids:
https://www.etsi.org/deliver/etsi_ts/102000_102099/102034/01.02.01_60/ts_102034v010201p.pdf

The application panicked (crashed).                                                                                                                                                           Message:  called `Result::unwrap()` on an `Err` value: Type                                                                                                                                   
Location: /home/daniel/.cargo/registry/src/index.crates.io-6f17d22bba15001f/lopdf-0.32.0/src/toc.rs:71                                                                                                                                                                                                                                                                                      
  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ BACKTRACE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━                                                                                                                                            ⋮ 9 frames hidden ⋮                               
  10: core::result::Result::unwrap::h1dd6925ad870affb                                                                                                                                    
      at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/result.rs:1073
  11: lopdf::toc::setup_outline_page_ids::h8d4e621112de3625                                                                                                                                   
      at /home/daniel/.cargo/registry/src/index.crates.io-6f17d22bba15001f/lopdf-0.32.0/src/toc.rs:71
  12: lopdf::toc::::get_toc::h2f5fa8a1257fe404                                                                                                                
      at /home/daniel/.caThe application panicked (crashed).                                                                                                                                                           Message:  called `Result::unwrap()` on an `Err` value: Type                                                                                                                                   
Location: /home/daniel/.cargo/registry/src/index.crates.io-6f17d22bba15001f/lopdf-0.32.0/src/toc.rs:71                                                                                                                                                                                                                                                                                      
  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ BACKTRACE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━                                                                                                                                            ⋮ 9 frames hidden ⋮                               
rgo/registry/src/index.crates.io-6f17d22bba15001f/lopdf-0.32.0/src/toc.rs:99
  13: getsi::pdf::lopdf::get_toc::h528e8dc44864dd4d                                                                                                                                           
      at /home/daniel/scm/osmo/rust/getsi/src/pdf/lopdf.rs:7       

Opening the file in evince shows that some chapters are missing pages (4 and 4.1) in the index view.
It looks like the second unwrap in the line fails and destination.page().unwrap().as_reference() returns an error

@Heinenen
Copy link
Collaborator

Heinenen commented Aug 3, 2024

Reposting the PDF in case the link goes dead:
ts_102034v010201p.pdf

@Heinenen
Copy link
Collaborator

Heinenen commented Aug 5, 2024

The panic happens because the PDF is malformed, cf. PDF 1.7, table 151 Destination syntax.

Two of the contained /Dest entries in the document outline contain null instead of an indirect reference.
This causes a "type error" when trying to call as_reference() on the null value.

In a sense, the panic is thus expected behavior. In the future, lopdf should probably handle similar errors more gracefully by returning in error instead of panicking, discussion on this probably should probably be held in #285.

@bastibense
Copy link

bastibense commented Sep 16, 2024

+1

I'm having the same problem.

Some user provided PDFs don't contain a proper TOC structure (or in my case, the references to destinations are broken).

The PDF in question is somehow broken in more than one way - and the problem should be fixed by using a proper PDF file - but still I'd like to be able to handle the error properly in my code and unwind my code properly and also display a proper error message.

Right now it will just panic randomly when calling pdf_doc.get_toc(), which is not very handy.

However, valid files work like a breeze, which is awesome.

The offending code in toc.rs:

fn setup_outline_page_ids<'a>(
    outlines: &'a Vec<Outline>, result: &mut OutlinePageIds, level: usize,
) -> &'a Vec<Outline> {
    for outline in outlines.iter() {
        match outline {
            Outline::Destination(destination) => {

// The following unwraps() should probably return an Err instead.

                result.insert(
                    destination.title().unwrap().as_str().unwrap().to_vec(),
                    (destination.page().unwrap().as_reference().unwrap(), result.len(), level),
                );
            }
            Outline::SubOutlines(sub_outlines) => {
                setup_outline_page_ids(sub_outlines, result, level + 1);
            }
        }
    }
    outlines
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants