Skip to content

Cyclic references in Set objects raise exception with YAML#load #8352

@mcorino

Description

@mcorino

Environment Information

JRuby version:

jruby 9.4.8.0 (3.1.4) 2024-07-02 4d41e55a67 OpenJDK 64-Bit Server VM 17.0.12+7-suse-150400.3.45.1-x8664 on 17.0.12+7-suse-150400.3.45.1-x8664 +jit [x86_64-linux]

OS:

Linux 5.14.21-150500.55.73-default #1 SMP PREEMPT_DYNAMIC Tue Aug 6 15:51:33 UTC 2024 (a0ede6a) x86_64 x86_64 x86_64 GNU/Linux

Creating a Set object with a cyclic reference and serializing with YAML and subsequently deserializing will result in an exception in YAML#load as can be reproduced by the following code snippit:

require 'set'
require 'yaml'

set = Set.new([:one, :two])
set << set
s = set.to_yaml
YAML.load(s, permitted_classes: [Set, Symbol], aliases: true)

Expected Behavior

As the JRuby YAML implementation correctly serializes the Set object and it's cyclic reference I expected deserialization to work correctly as well as it does in CRuby/MRI.

Actual Behavior

The following exception is raised:

org/jruby/ext/psych/PsychParser.java:308:in `_native_parse': (<unknown>): could not find expected ':' while scanning a simple key at line 6 column 1 (Psych::SyntaxError)
        from /home/martin/.rvm/rubies/jruby-9.4.8.0/lib/ruby/stdlib/psych/parser.rb:62:in `parse'
        from /home/martin/.rvm/rubies/jruby-9.4.8.0/lib/ruby/stdlib/psych.rb:455:in `parse_stream'
        from /home/martin/.rvm/rubies/jruby-9.4.8.0/lib/ruby/stdlib/psych.rb:399:in `parse'
        from /home/martin/.rvm/rubies/jruby-9.4.8.0/lib/ruby/stdlib/psych.rb:323:in `safe_load'
        from /home/martin/.rvm/rubies/jruby-9.4.8.0/lib/ruby/stdlib/psych.rb:369:in `load'
        from (irb):7:in `evaluate'
        from org/jruby/RubyKernel.java:1274:in `eval'
        from org/jruby/RubyKernel.java:1722:in `loop'
        from org/jruby/RubyKernel.java:1423:in `catch'
        from org/jruby/RubyKernel.java:1423:in `catch'
        from /home/martin/.rvm/rubies/jruby-9.4.8.0/lib/ruby/gems/shared/gems/irb-1.4.2/exe/irb:11:in `<main>'
        from org/jruby/RubyKernel.java:1220:in `load'
        from /home/martin/.rvm/rubies/jruby-9.4.8.0/bin/irb:25:in `<main>'
        from org/jruby/RubyKernel.java:1274:in `eval'
        from /home/martin/.rvm/rubies/jruby-9.4.8.0/bin/jruby_executable_hooks:22:in `<main>'
org.snakeyaml.engine.v2.scanner.ScannerImpl.stalePossibleSimpleKeys(ScannerImpl.java:460): while scanning a simple key (Java::OrgSnakeyamlEngineV2Exceptions::ScannerException)
 in reader, line 5, column 3:
      *1: true
      ^
could not find expected ':'
 in reader, line 6, column 1:
    
    ^

        from org.snakeyaml.engine.v2.scanner.ScannerImpl.needMoreTokens(ScannerImpl.java:279)
        from org.snakeyaml.engine.v2.scanner.ScannerImpl.checkToken(ScannerImpl.java:191)
        from org.snakeyaml.engine.v2.parser.ParserImpl$ParseBlockMappingKey.produce(ParserImpl.java:711)
        from org.snakeyaml.engine.v2.parser.ParserImpl.lambda$produce$1(ParserImpl.java:232)
        from java.base/java.util.Optional.ifPresent(Optional.java:178)
        from org.snakeyaml.engine.v2.parser.ParserImpl.produce(ParserImpl.java:232)
        from org.snakeyaml.engine.v2.parser.ParserImpl.hasNext(ParserImpl.java:226)
        from org.jruby.ext.psych.PsychParser.parse(PsychParser.java:248)
        from org.jruby.ext.psych.PsychParser$INVOKER$i$3$0$parse.call(PsychParser$INVOKER$i$3$0$parse.gen)
        from org.jruby.dist/org.jruby.internal.runtime.methods.JavaMethod$JavaMethodThree.call(JavaMethod.java:1371)
        from org.jruby.dist/org.jruby.internal.runtime.methods.JavaMethod$JavaMethodN.call(JavaMethod.java:847)
        from org.jruby.dist/org.jruby.runtime.callsite.CachingCallSite.cacheAndCall(CachingCallSite.java:446)
        from org.jruby.dist/org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:92)
        from org.jruby.dist/org.jruby.ir.instructions.CallBase.interpret(CallBase.java:548)
        from org.jruby.dist/org.jruby.ir.interpreter.InterpreterEngine.processCall(InterpreterEngine.java:363)
        from org.jruby.dist/org.jruby.ir.interpreter.StartupInterpreterEngine.interpret(StartupInterpreterEngine.java:66)
        ... 197 levels...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions