Skip to content

Commit

Permalink
opening an empty file should raise a MalformedPDFError
Browse files Browse the repository at this point in the history
* add an integration spec to prove it
* closes #69
  • Loading branch information
yob committed Dec 19, 2012
1 parent 4da82e0 commit 8fb4186
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/pdf/reader/buffer.rb
Expand Up @@ -115,6 +115,9 @@ def token
# return the byte offset where the first XRef table in th source can be found.
#
def find_first_xref_offset
if @io.size == 0
raise MalformedPDFError, "PDF file is empty"
end
@io.seek(-1024, IO::SEEK_END) rescue @io.seek(0)
data = @io.read(1024)

Expand Down
2 changes: 1 addition & 1 deletion lib/pdf/reader/object_hash.rb
Expand Up @@ -41,8 +41,8 @@ class ObjectHash
#
def initialize(input, opts = {})
@io = extract_io_from(input)
@pdf_version = read_version
@xref = PDF::Reader::XRef.new(@io)
@pdf_version = read_version
@trailer = @xref.trailer
@cache = opts[:cache] || PDF::Reader::ObjectCache.new
@sec_handler = build_security_handler(opts)
Expand Down
2 changes: 2 additions & 0 deletions lib/pdf/reader/xref.rb
Expand Up @@ -237,6 +237,8 @@ def calc_junk_offset(io)
end
io.rewind
offset < 50 ? offset : nil
rescue EOFError
return nil
end
end
################################################################################
Expand Down
6 changes: 6 additions & 0 deletions spec/integration_spec.rb
Expand Up @@ -134,6 +134,12 @@
}.should raise_error(PDF::Reader::MalformedPDFError)
end

it "should raise an exception if the file is empty" do
lambda {
PDF::Reader.new(StringIO.new(""))
}.should raise_error(PDF::Reader::MalformedPDFError)
end

it "should correctly process a PDF that uses an ASCII85Decode filter" do
filename = pdf_spec_file("ascii85_filter")

Expand Down

0 comments on commit 8fb4186

Please sign in to comment.