Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

undefined method `match' for nil:NilClass when passing file of 0 Bytes #69

zedtux opened this Issue Dec 19, 2012 · 3 comments


None yet
2 participants

zedtux commented Dec 19, 2012


When using a PDF file of size 0 Bytes, we got the following error:

undefined method `match' for nil:NilClass

The error is raise from object_hash.rb @ line 326.

Find the stack trace:

undefined method `match' for nil:NilClass
/srv/fundlook_testing/shared/bundle/ruby/1.8/gems/pdf-reader-1.0.0/lib/pdf/reader/object_hash.rb:326:in `read_version'
/srv/fundlook_testing/shared/bundle/ruby/1.8/gems/pdf-reader-1.0.0/lib/pdf/reader/object_hash.rb:44:in `initialize'
/srv/fundlook_testing/shared/bundle/ruby/1.8/gems/pdf-reader-1.0.0/lib/pdf/reader.rb:116:in `new'
/srv/fundlook_testing/shared/bundle/ruby/1.8/gems/pdf-reader-1.0.0/lib/pdf/reader.rb:116:in `initialize'

zedtux commented Dec 19, 2012

I changed the initialize of ObjectHash like this:

    def initialize(input, opts = {})
      @io          = extract_io_from(input)
      unless @io.size == 0
        @pdf_version = read_version
        @xref        = PDF::Reader::XRef.new(@io)
        @trailer     = @xref.trailer
        @sec_handler = build_security_handler(opts)
      @cache       = PDF::Reader::ObjectCache.new

Then it doesn't fail anymore, but I don't know if it's the right way (I didn't took time to search/write an RSpec test now):

>> PDF::Reader.new("/srv/fundlook_testing/current/uploads/1717/empty_file.pdf")
=> #<PDF::Reader:0x10ba9608 @objects=#<PDF::Reader::ObjectHash:0x10ba9590 @io=#<StringIO:0x10ba9450>, @cache=#<PDF::Reader::ObjectCache:0x10ba9540 @objects={}>>>

@yob yob closed this in 8fb4186 Dec 19, 2012


yob commented Dec 19, 2012

Hi, thanks for reporting this.

An empty file isn't a valid PDF, so the correct behaviour is to raise a PDF::Reader::MalformedPDFError. I've fixed this and added a spec to prevent regressions.

zedtux commented Dec 20, 2012

Nice and fast fix! Awesome!

Waiting for rubygems.org update! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment