You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, thanks for your work on this great tool. I'm using the pdfcpu API (latest release, 0.4.2) and hit a minor snag with the AddBookmarks/AddBookmarksFile routines. The issue appears to be independent of the input PDF — it occurred for all the files I tried it with.
Summary
The following call to api.AddBookmarks works as expected:
but the following addition (setting Children to an empty but non-nil slice) results in a runtime error, panic: runtime error: invalid memory address or nil pointer dereference:
first, last, c, visc, err:=createOutlineItemDict(ctx, bm.Children, ir, &bm.PageFrom)
However, this seems problematic if bm.Children is a non-nil empty slice: createOutlineItemDict(..., bm.Children, ...) will return two nil IndirectRefs, so first and last will be nil. This then causes the observed panic when first and last are later dereferenced:
and indeed the stack trace points to line 301 as well. The likely fix (untested) is to change bm.Children != nil to len(bm.Children) == 0.
Context
This issue has an easy workaround; just always pass nil slices instead of empty but non-nil ones. However, in my use-case, this is minorly inconvenient: the bookmarks are generated dynamically, and so I need a bit of extra code to handle this scenario:
iflen(out.Children) ==0 {
out.Children=nil
}
Since the fix appears reasonably simple, I think it's worth fixing this issue — unless, of course, there's a reason for not allowing empty slices that I'm missing.
Thanks!
The text was updated successfully, but these errors were encountered:
Hi, thanks for your work on this great tool. I'm using the pdfcpu API (latest release, 0.4.2) and hit a minor snag with the
AddBookmarks
/AddBookmarksFile
routines. The issue appears to be independent of the input PDF — it occurred for all the files I tried it with.Summary
The following call to
api.AddBookmarks
works as expected:but the following addition (setting
Children
to an empty but non-nil slice) results in a runtime error,panic: runtime error: invalid memory address or nil pointer dereference
:bm := pdfcpu.Bookmark{ PageFrom: 1, Title: "foobar", + Children: []pdfcpu.Bookmark{}, } err := api.AddBookmarksFile("in.pdf", "out.pdf", []pdfcpu.Bookmark{bm}, true, nil) if err != nil { panic(err) }
Show full stack trace
Reproduction Steps
pdfcpu
:main.go
; this is the same as the code provided above with the relevant imports added so it can be run directly.Show code
in.pdf
; for this issue report I usedCenterOfWhy.pdf
from your test suite: https://github.com/pdfcpu/pdfcpu/blob/master/pkg/testdata/CenterOfWhy.pdf.go run .
; observe that no runtime error occurs.Children: []pdfcpu.Bookmark{}
and executego run .
again; observe the runtime error.Analysis
The source of the issue seems to be the
createOutlineItemDict
function:pdfcpu/pkg/pdfcpu/bookmark.go
Line 259 in a9afcfe
Specifically, for each bookmark
bm
given,createOutlineItemDict
checks ifbm.Children
is non-nil and if so recurses onbm.Children
:pdfcpu/pkg/pdfcpu/bookmark.go
Lines 294 to 296 in a9afcfe
However, this seems problematic if
bm.Children
is a non-nil empty slice:createOutlineItemDict(..., bm.Children, ...)
will return two nilIndirectRef
s, sofirst
andlast
will be nil. This then causes the observed panic whenfirst
andlast
are later dereferenced:pdfcpu/pkg/pdfcpu/bookmark.go
Lines 301 to 302 in a9afcfe
and indeed the stack trace points to line 301 as well. The likely fix (untested) is to change
bm.Children != nil
tolen(bm.Children) == 0
.Context
This issue has an easy workaround; just always pass nil slices instead of empty but non-nil ones. However, in my use-case, this is minorly inconvenient: the bookmarks are generated dynamically, and so I need a bit of extra code to handle this scenario:
Since the fix appears reasonably simple, I think it's worth fixing this issue — unless, of course, there's a reason for not allowing empty slices that I'm missing.
Thanks!
The text was updated successfully, but these errors were encountered: