Skip to content
This repository
Browse code

opening an empty file should raise a MalformedPDFError

* add an integration spec to prove it
* closes #69
  • Loading branch information...
commit 8fb418608ebebdc6a7086aab8a96808c0a002577 1 parent 4da82e0
James Healy authored December 19, 2012
3  lib/pdf/reader/buffer.rb
@@ -115,6 +115,9 @@ def token
115 115
     # return the byte offset where the first XRef table in th source can be found.
116 116
     #
117 117
     def find_first_xref_offset
  118
+      if @io.size == 0
  119
+        raise MalformedPDFError, "PDF file is empty"
  120
+      end
118 121
       @io.seek(-1024, IO::SEEK_END) rescue @io.seek(0)
119 122
       data = @io.read(1024)
120 123
 
2  lib/pdf/reader/object_hash.rb
@@ -41,8 +41,8 @@ class ObjectHash
41 41
     #
42 42
     def initialize(input, opts = {})
43 43
       @io          = extract_io_from(input)
44  
-      @pdf_version = read_version
45 44
       @xref        = PDF::Reader::XRef.new(@io)
  45
+      @pdf_version = read_version
46 46
       @trailer     = @xref.trailer
47 47
       @cache       = opts[:cache] || PDF::Reader::ObjectCache.new
48 48
       @sec_handler = build_security_handler(opts)
2  lib/pdf/reader/xref.rb
@@ -237,6 +237,8 @@ def calc_junk_offset(io)
237 237
       end
238 238
       io.rewind
239 239
       offset < 50 ? offset : nil
  240
+    rescue EOFError
  241
+      return nil
240 242
     end
241 243
   end
242 244
   ################################################################################
6  spec/integration_spec.rb
@@ -134,6 +134,12 @@
134 134
     }.should raise_error(PDF::Reader::MalformedPDFError)
135 135
   end
136 136
 
  137
+  it "should raise an exception if the file is empty" do
  138
+    lambda {
  139
+      PDF::Reader.new(StringIO.new(""))
  140
+    }.should raise_error(PDF::Reader::MalformedPDFError)
  141
+  end
  142
+
137 143
   it "should correctly process a PDF that uses an ASCII85Decode filter" do
138 144
     filename = pdf_spec_file("ascii85_filter")
139 145
 

0 notes on commit 8fb4186

Please sign in to comment.
Something went wrong with that request. Please try again.