Skip to content

Commit

Permalink
Disallow anonymous classes
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Sep 9, 2023
1 parent 29833da commit 703b6a5
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions core/src/main/java/org/jruby/runtime/marshal/MarshalStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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<Variable<Object>> vars, IRubyObject obj) throws IOException {
if (shouldMarshalEncoding(obj)) {
writeInt(vars.size() + 1); // vars preceded by encoding
Expand Down

0 comments on commit 703b6a5

Please sign in to comment.