Skip to content

Commit 2c3610f

Browse files
committed
Adding finalizer for invalid PDF file input so that the file is closed.
1 parent 107f921 commit 2c3610f

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/CosDoc.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ mutable struct CosDocImpl <: CosDoc
6666
io = util_open(fp,"r")
6767
sz = filesize(fp)
6868
ps = io
69-
new(fp, sz, io, ps, 0, "", 0, (0, 0),
70-
Dict{CosIndirectObjectRef, CosObjectLoc}(),
71-
[], [], [], CosNull, false, false, nothing, 0)
69+
this = new(fp, sz, io, ps, 0, "", 0, (0, 0),
70+
Dict{CosIndirectObjectRef, CosObjectLoc}(),
71+
[], [], [], CosNull, false, false, nothing, 0)
72+
finalizer(x->util_close(x.ps), this)
73+
return this
7274
end
7375
end
7476

@@ -113,7 +115,6 @@ called if you have opened the document by 'cosDocOpen'. Documents opened with
113115
`pdDocOpen` do not need to use this method.
114116
"""
115117
function cosDocClose(doc::CosDocImpl)
116-
util_close(doc.ps)
117118
for path in doc.tmpfiles
118119
rm(path)
119120
end
@@ -132,6 +133,7 @@ function cosDocOpen(fp::AbstractString; access::Function=identity)
132133
doc = CosDocImpl(abspath(fp))
133134
ps = doc.ps
134135
h = read_header(ps)
136+
h[1] == 0 && error(E_BAD_HEADER)
135137
doc.version = (h[1], h[2])
136138
doc.header = String(h[3])
137139
doc.hoffset = h[4]

0 commit comments

Comments
 (0)