summaryrefslogtreecommitdiff
path: root/marshal.c
diff options
context:
space:
mode:
authorNARUSE, Yui <naruse@airemix.jp>2023-03-08 14:46:30 +0900
committerNARUSE, Yui <naruse@airemix.jp>2023-03-08 14:46:30 +0900
commit4e4a4e42b284d9309a7e51c97058093539e7a843 (patch)
treece2710dc195ee49e8f41de8b2b68af9b4ca50b23 /marshal.c
parent59eb18037ff92839be48fb6c46ff0acc179b4f4c (diff)
merge revision(s) d2520b7b76759118071a16e6bca22726a5de9fb4: [Backport #19439]
Marshal.load: restore instance variables on Regexp [Bug #19439] The instance variables were restore on the Regexp source, not the regexp itself. Unfortunately we have a bit of a chicken and egg problem. The source holds the encoding, and the encoding need to be set on the source to be able to instantiate the Regexp. So the instance variables have to be read on the `source`. To correct this we transfert the instance variables after instantiating the Regexp. The only way to avoid this would be to read the instance variable twice and rewind. --- marshal.c | 20 ++++++++++++++++++-- spec/ruby/core/marshal/shared/load.rb | 11 +++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-)
Diffstat (limited to 'marshal.c')
-rw-r--r--marshal.c20
1 files changed, 18 insertions, 2 deletions
diff --git a/marshal.c b/marshal.c
index 7152be2924..25b693d8da 100644
--- a/marshal.c
+++ b/marshal.c
@@ -1803,6 +1803,20 @@ r_object0(struct load_arg *arg, bool partial, int *ivp, VALUE extmod)
return r_object_for(arg, partial, ivp, extmod, type);
}
+static int
+r_move_ivar(st_data_t k, st_data_t v, st_data_t d)
+{
+ ID key = (ID)k;
+ VALUE value = (VALUE)v;
+ VALUE dest = (VALUE)d;
+
+ if (rb_is_instance_id(key)) {
+ rb_ivar_set(dest, key, value);
+ return ST_DELETE;
+ }
+ return ST_CONTINUE;
+}
+
static VALUE
r_object_for(struct load_arg *arg, bool partial, int *ivp, VALUE extmod, int type)
{
@@ -1826,7 +1840,6 @@ r_object_for(struct load_arg *arg, bool partial, int *ivp, VALUE extmod, int typ
case TYPE_IVAR:
{
int ivar = TRUE;
-
v = r_object0(arg, true, &ivar, extmod);
if (ivar) r_ivar(v, NULL, arg);
v = r_leave(v, arg, partial);
@@ -2019,7 +2032,10 @@ r_object_for(struct load_arg *arg, bool partial, int *ivp, VALUE extmod, int typ
}
rb_str_set_len(str, dst - ptr);
}
- v = r_entry0(rb_reg_new_str(str, options), idx, arg);
+ VALUE regexp = rb_reg_new_str(str, options);
+ rb_ivar_foreach(str, r_move_ivar, regexp);
+
+ v = r_entry0(regexp, idx, arg);
v = r_leave(v, arg, partial);
}
break;