-
-
Notifications
You must be signed in to change notification settings - Fork 579
Description
- Your issue is based on the latest commit
- Yes
- State your OS and OS version
- macOS Ventura 13.6.3
Summary
I identified a stack overflow error when merging (and also bookmarking) pdfs. This appears to come from the optimization code, primarily barfing out when looping in the EqualObjects recursive function call. There is no workaround at this time besides forking the pdfcpu repository and making some adjustments. I also identified a missing nil check in the watermark API.
Stack overflow from merge and bookmark
From the EqualObjects func, you can trigger this overflow by downloading these files ("outlines-with-loop.pdf" and "sample.pdf").
Then by running this command
pdfcpu merge -m create output.pdf outlines-with-loop.pdf sample.pdf
For the bookmark overflow, I haven't recreated it from CLI but have recreated it from API call. If you take these same files, merge them together, and then add bookmarks to them (I managed to do this after disabling optimization for merging in a fork, but not disabling optimization in bookmarks yet). I did this via the AddBookmarks API func.
Watermark and merge nil check
In the AddWatermarksSliceMap func, this loop will cause a nil pointer ref error if the m map[int][]*model.Watermark parameter is not-nil for m, but contains only nil slices.
Example, m is not nil, but its slices are
m := make(map[int][]*Watermark)
m[1] = nilA separate missing nil check was also found in pkg/pdfcpu/merge.go from *f and *l not being nil checked.
d["First"] = *f
d["Last"] = *lNo workaround
It wasn't possible to workaround the overflow error as "OptimizeBeforeWriting" hadn't been added to config.yml yet per pdfcpu TODO, as well as the AddBookmarks func did not do a config check for optimization before calling ReadValidateAndOptimize.
Fork
Here is the git diff of our fork and pdfcpu that we made to work around these issues.
I hope this helps identify and resolve these issues as we greatly look forward to switching from our fork back to the official pdfcpu repository.