ruby / ruby/psych

psych fails to load YAML containing serialized objects for which `klass.allocate` returns nil

Open
#247 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
597
Forks
223
Avg merge
11h 23m
Merged PRs (30d)
3

Description

The beaker testing library serializes a YAML file representing the configuration for a prior test run. That library can contain AWS::Core::Data objects (see http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/Core/Data.html).

For example, a snippet might look like:

        :placement: !ruby/object:AWS::Core::Data
          data:
            :availability_zone: us-west-2a
            :group_name:
            :tenancy: default

The problem is that AWS::Core::Data somehow manages to have its .allocate method return nil:

>> require 'aws/core/data'
=> true
>> AWS::Core::Data.allocate
=> nil

When this is de-serialized, s here becomes nil: https://github.com/tenderlove/psych/blob/master/lib/psych/visitors/to_ruby.rb#L371 and then fireworks happen here: https://github.com/tenderlove/psych/blob/master/lib/psych/visitors/to_ruby.rb#L379

The end of the stack trace looks like:

/home/rick/pl/pe_acceptance_tests/vendor/ruby/gems/aws-sdk-v1-1.65.0/lib/aws/core/data.rb:146:in `respond_to?': undefined method `key?' for nil:NilClass (NoMethodError)
    from /home/rick/pl/pe_acceptance_tests/vendor/ruby/gems/psych-2.0.15/lib/psych/visitors/to_ruby.rb:379:in `init_with'
    from /home/rick/pl/pe_acceptance_tests/vendor/ruby/gems/psych-2.0.15/lib/psych/visitors/to_ruby.rb:372:in `revive'
    from /home/rick/pl/pe_acceptance_tests/vendor/ruby/gems/psych-2.0.15/lib/psych/visitors/to_ruby.rb:207:in `visit_Psych_Nodes_Mapping'
    from /home/rick/pl/pe_acceptance_tests/vendor/ruby/gems/psych-2.0.15/lib/psych/visitors/visitor.rb:15:in `visit'
    from /home/rick/pl/pe_acceptance_tests/vendor/ruby/gems/psych-2.0.15/lib/psych/visitors/visitor.rb:5:in `accept'
    from /home/rick/pl/pe_acceptance_tests/vendor/ruby/gems/psych-2.0.15/lib/psych/visitors/to_ruby.rb:31:in `accept'
    from /home/rick/pl/pe_acceptance_tests/vendor/ruby/gems/psych-2.0.15/lib/psych/visitors/to_ruby.rb:336:in `block in revive_hash'

I don't think AWS::Core::Data is being a good ruby citizen by doing whatever they're doing with .allocate; but perhaps the error thrown here good be more intuitive. We may work around this with a to_yaml at serialization time (or something similar), but figured this was worth reporting.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with lib/psych/visitors/to_ruby.rb around lines 371 and 379, then reproduce deserialization using the AWS::Core::Data YAML example and the reported Psych stack trace. Determine how the nil result from klass.allocate should be handled so the failure is more intuitive, and verify that the YAML no longer produces the reported NoMethodError.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.