diff options
| author | Takashi Kokubun <takashikkbn@gmail.com> | 2026-04-03 20:16:11 -0700 |
|---|---|---|
| committer | Takashi Kokubun <takashikkbn@gmail.com> | 2026-04-03 20:16:11 -0700 |
| commit | 4fa4d71d751a4efeb5616ca7227fa57df0e718b0 (patch) | |
| tree | 946d4c9e88c6a57779922a8d87363ad8843caac1 | |
| parent | 6b7f5aa73199616fd438847059187a95c0c6ef0f (diff) | |
merge revision(s) 54c4694994cc3bcfea9058b22ba3e68af6aaf740: [Backport #21961]
marshal.c: properly freeze linked strings
| -rw-r--r-- | marshal.c | 3 | ||||
| -rw-r--r-- | test/ruby/test_marshal.rb | 14 |
2 files changed, 17 insertions, 0 deletions
@@ -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 |
