summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakashi Kokubun <takashikkbn@gmail.com>2026-04-03 20:16:11 -0700
committerTakashi Kokubun <takashikkbn@gmail.com>2026-04-03 20:16:11 -0700
commit4fa4d71d751a4efeb5616ca7227fa57df0e718b0 (patch)
tree946d4c9e88c6a57779922a8d87363ad8843caac1
parent6b7f5aa73199616fd438847059187a95c0c6ef0f (diff)
merge revision(s) 54c4694994cc3bcfea9058b22ba3e68af6aaf740: [Backport #21961]
marshal.c: properly freeze linked strings
-rw-r--r--marshal.c3
-rw-r--r--test/ruby/test_marshal.rb14
2 files changed, 17 insertions, 0 deletions
diff --git a/marshal.c b/marshal.c
index 8cd4dc6079..a94d90fbbe 100644
--- a/marshal.c
+++ b/marshal.c
@@ -1879,6 +1879,9 @@ r_object_for(struct load_arg *arg, bool partial, int *ivp, VALUE extmod, int typ
}
v = (VALUE)link;
if (!st_lookup(arg->partial_objects, (st_data_t)v, &link)) {
+ if (arg->freeze && RB_TYPE_P(v, T_STRING)) {
+ v = rb_str_to_interned_str(v);
+ }
v = r_post_proc(v, arg);
}
break;
diff --git a/test/ruby/test_marshal.rb b/test/ruby/test_marshal.rb
index eb66994801..05d3ceb60a 100644
--- a/test/ruby/test_marshal.rb
+++ b/test/ruby/test_marshal.rb
@@ -1002,5 +1002,19 @@ class TestMarshal < Test::Unit::TestCase
refute_predicate Object, :frozen?
refute_predicate Kernel, :frozen?
end
+
+ def test_linked_strings_are_frozen
+ str = "test"
+ str.instance_variable_set(:@self, str)
+ source = [str, str]
+
+ objects = Marshal.load(encode(source), freeze: true)
+ assert_predicate objects[0], :frozen?
+ assert_predicate objects[1], :frozen?
+ assert_same objects[0], objects[1]
+ assert_same objects[0], objects[0].instance_variable_get(:@self)
+ assert_same objects[1], objects[1].instance_variable_get(:@self)
+ assert_same objects[0].instance_variable_get(:@self), objects[1].instance_variable_get(:@self)
+ end
end
end