Permalink
Browse files

opening an empty file should raise a MalformedPDFError

* add an integration spec to prove it
* closes #69
  • Loading branch information...
1 parent 4da82e0 commit 8fb418608ebebdc6a7086aab8a96808c0a002577 @yob committed Dec 19, 2012
Showing with 12 additions and 1 deletion.
  1. +3 −0 lib/pdf/reader/buffer.rb
  2. +1 −1 lib/pdf/reader/object_hash.rb
  3. +2 −0 lib/pdf/reader/xref.rb
  4. +6 −0 spec/integration_spec.rb
View
@@ -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)
@@ -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)
View
@@ -237,6 +237,8 @@ def calc_junk_offset(io)
end
io.rewind
offset < 50 ? offset : nil
+ rescue EOFError
+ return nil
end
end
################################################################################
View
@@ -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")

0 comments on commit 8fb4186

Please sign in to comment.