Skip to content

Commit

Permalink
fix class clone bug
Browse files Browse the repository at this point in the history
fix freeze/taint bug for cloned/dup'd objects
add test cases


git-svn-id: http://svn.codehaus.org/jruby/trunk/jruby@1526 961051c9-f516-0410-bf72-c9f7e237a7b7
  • Loading branch information
Stefan Matthias Aust committed Dec 22, 2004
1 parent bcba816 commit 49aa6e5
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/org/jruby/RubyClass.java
Expand Up @@ -292,4 +292,8 @@ public RubyClass newSubClass(String name, RubyModule parentModule) {

return newClass;
}

protected IRubyObject doClone() {
return RubyClass.newClass(getRuntime(), getSuperClass(), null/*FIXME*/, getBaseName());
}
}
2 changes: 2 additions & 0 deletions src/org/jruby/RubyHash.java
Expand Up @@ -308,7 +308,9 @@ public RubyString to_s() {

public IRubyObject rbClone() {
RubyHash result = newHash(getRuntime(), getValueMap(), getDefaultValue());
result.setTaint(isTaint());
result.initCopy(this);
result.setFrozen(isFrozen());
return result;
}

Expand Down
6 changes: 5 additions & 1 deletion src/org/jruby/RubyModule.java
Expand Up @@ -977,14 +977,18 @@ public IRubyObject rbClone() {
}
return clone;
}

protected IRubyObject doClone() {
return RubyModule.newModule(getRuntime(), classId, parentModule);
}

/** rb_mod_dup
*
*/
public IRubyObject dup() {
RubyModule dup = (RubyModule) rbClone();
dup.setMetaClass(getMetaClass());

dup.setFrozen(false);
// +++ jpetersen
// dup.setSingleton(isSingleton());
// --- jpetersen
Expand Down
14 changes: 9 additions & 5 deletions src/org/jruby/RubyObject.java
Expand Up @@ -702,16 +702,20 @@ public RubyClass type_deprecated() {
*
*/
public IRubyObject rbClone() {
IRubyObject clone = getMetaClass().getRealClass().allocate();
IRubyObject clone = doClone();
clone.setMetaClass(getMetaClass().getSingletonClassClone());
clone.setFrozen(false);
clone.setTaint(this.isTaint());
clone.initCopy(this);
if (isFrozen()) {
clone.setFrozen(true);
}
clone.setFrozen(isFrozen());
return clone;
}

// Hack: allow RubyModule and RubyClass to override the allocation and return the the correct Java instance
// Cloning a class object doesn't work otherwise and I don't really understand why --sma
protected IRubyObject doClone() {
return getMetaClass().getRealClass().allocate();
}

public IRubyObject display(IRubyObject[] args) {
IRubyObject port = args.length == 0
? getRuntime().getGlobalVariables().get("$>") : args[0];
Expand Down
4 changes: 3 additions & 1 deletion src/org/jruby/RubyRegexp.java
Expand Up @@ -451,7 +451,9 @@ public IRubyObject rbClone() {
RubyRegexp newObj = new RubyRegexp(getRuntime());
newObj.pattern = pattern;
newObj.code = code;
initCopy(newObj);
newObj.setTaint(isTaint());
newObj.initCopy(this);
newObj.setFrozen(isFrozen());
return newObj;
}

Expand Down
3 changes: 1 addition & 2 deletions src/org/jruby/RubyString.java
Expand Up @@ -192,9 +192,8 @@ public IRubyObject dup() {
*/
public IRubyObject rbClone() {
IRubyObject newObject = dup();

newObject.initCopy(this);

newObject.setFrozen(isFrozen());
return newObject;
}

Expand Down
37 changes: 37 additions & 0 deletions test/testDupCloneTaintFreeze.rb
@@ -0,0 +1,37 @@
require 'test/minirunit'

test_check "Test Taint"

test_ok(Module.new.taint.dup.tainted?)
test_ok(Class.new.taint.dup.tainted?)
test_ok(Object.new.taint.dup.tainted?)
test_ok(String.new.taint.dup.tainted?)
test_ok([].taint.dup.tainted?)
test_ok({}.taint.dup.tainted?)
test_ok(//.taint.dup.tainted?)

test_ok(Module.new.taint.clone.tainted?)
test_ok(Class.new.taint.clone.tainted?)
test_ok(Object.new.taint.clone.tainted?)
test_ok(String.new.taint.clone.tainted?)
test_ok([].taint.clone.tainted?)
test_ok({}.taint.clone.tainted?)
test_ok(//.taint.clone.tainted?)

test_check "Test Freeze"

test_ok(!Module.new.freeze.dup.frozen?)
test_ok(!Class.new.freeze.dup.frozen?)
test_ok(!Object.new.freeze.dup.frozen?)
test_ok(!String.new.freeze.dup.frozen?)
test_ok(![].freeze.dup.frozen?)
test_ok(!{}.freeze.dup.frozen?)
test_ok(!//.freeze.dup.frozen?)

test_ok(Module.new.freeze.clone.frozen?)
test_ok(Class.new.freeze.clone.frozen?)
test_ok(Object.new.freeze.clone.frozen?)
test_ok(String.new.freeze.clone.frozen?)
test_ok([].freeze.clone.frozen?)
test_ok({}.freeze.clone.frozen?)
test_ok(//.freeze.clone.frozen?)
1 change: 1 addition & 0 deletions test/test_index
Expand Up @@ -43,6 +43,7 @@ testCase.rb
testLine.rb
testJavaProxy.rb
testRedefine.rb
testDupCloneTaintFreeze.rb

# MRI Ruby tests (from sample/test.rb in Matz's Ruby Interpreter):

Expand Down

0 comments on commit 49aa6e5

Please sign in to comment.