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

Infinite loop in Page() #29

Open
splitbrain opened this issue Jun 20, 2023 · 2 comments
Open

Infinite loop in Page() #29

splitbrain opened this issue Jun 20, 2023 · 2 comments

Comments

@splitbrain
Copy link

I have a PDF (which unfortunately I am not allowed to share) that causes an infinite loop in the Page() function.

Basically, none of the two if checks in the kid handling return true and the outer loop just keeps repeating

I'm not a go developer and have no idea about the inner workings of the PDF format, so I don't know how to address this.

Here are two screenshots from the debugger showing the state within the function:

2023-06-20-102905_387x551_scrot

2023-06-20-103316_262x121_scrot

PS I realize that this is probably a problem with the PDF, but I would prefer an error instead of an infinite loop.

@splitbrain
Copy link
Author

@RebotPtyLtd this problem is also still present in your fork (but I can't report it there because you don't have issues enabled)

@danieldids
Copy link

danieldids commented Nov 7, 2023

I'm facing the same issue. I have a sample PDF that I can share, would it help?

lo9922023_654a7ab402fcb.pdf

Yes, the file is a single page PDF, without any text whatsoever. For my purpose, it doesn't matter, all I need is some return, even if empty.

I've located the loop to be happening at this function bellow:

// Page returns the page for the given page number.
// Page numbers are indexed starting at 1, not 0.
// If the page is not found, Page returns a Page with p.V.IsNull().
func (r *Reader) Page(num int) Page {
	num-- // now 0-indexed
	page := r.Trailer().Key("Root").Key("Pages")
Search:
	for page.Key("Type").Name() == "Pages" {
		count := int(page.Key("Count").Int64())
		if count < num {
			return Page{}
		}
		kids := page.Key("Kids")
		for i := 0; i < kids.Len(); i++ {
			kid := kids.Index(i)
			if kid.Key("Type").Name() == "Pages" {
				c := int(kid.Key("Count").Int64())
				if num < c {
					page = kid
					continue Search
				}
				num -= c
				continue
			}
			if kid.Key("Type").Name() == "Page" {
				if num == 0 {
					return Page{kid}
				}
				num--
			}
		}
	}
	return Page{}
}

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

No branches or pull requests

2 participants