From 703b6a54b5126a49abe7c0a546ec60c683384fce Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Fri, 8 Sep 2023 15:59:31 +0200 Subject: [PATCH] Disallow anonymous classes --- .../jruby/runtime/marshal/MarshalStream.java | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/org/jruby/runtime/marshal/MarshalStream.java b/core/src/main/java/org/jruby/runtime/marshal/MarshalStream.java index 33732a9c5da..55b6b95dd8c 100644 --- a/core/src/main/java/org/jruby/runtime/marshal/MarshalStream.java +++ b/core/src/main/java/org/jruby/runtime/marshal/MarshalStream.java @@ -390,7 +390,12 @@ private void userCommon(IRubyObject value, CacheEntry entry) throws IOException write(TYPE_USERDEF); - writeAndRegisterSymbol(RubySymbol.newSymbol(runtime, klass.getRealClass().getName()).getBytes()); + // must_not_be_anonymous + RubyClass type = klass.getRealClass(); + String path = type.getName(); + mustNotBeAnonymous(runtime, type.isClass() ? "class" : "module", path); + + writeAndRegisterSymbol(RubySymbol.newSymbol(runtime, path).getBytes()); writeString(marshaled.getByteList()); @@ -404,17 +409,20 @@ private void userCommon(IRubyObject value, CacheEntry entry) throws IOException public void writeUserClass(IRubyObject obj, RubyClass type) throws IOException { write(TYPE_UCLASS); - // w_unique - if (type.getName().charAt(0) == '#') { - Ruby runtime = obj.getRuntime(); + // must_not_be_anonymous + String path = type.getName(); + mustNotBeAnonymous(runtime, type.isClass() ? "class" : "module", path); - throw runtime.newTypeError(str(runtime, "can't dump anonymous class ", types(runtime, type))); - } - // w_symbol - writeAndRegisterSymbol(RubySymbol.newSymbol(runtime, type.getName()).getBytes()); + writeAndRegisterSymbol(RubySymbol.newSymbol(runtime, path).getBytes()); } - + + private static void mustNotBeAnonymous(Ruby runtime, String clsOrMod, String path) { + if (path.charAt(0) == '#') { + throw runtime.newTypeError("can't dump anonymous " + clsOrMod + " " + path); + } + } + public void dumpVariablesWithEncoding(List> vars, IRubyObject obj) throws IOException { if (shouldMarshalEncoding(obj)) { writeInt(vars.size() + 1); // vars preceded by encoding