ruby-changes:71185
From: Koichi <ko1@a...>
Date: Wed, 16 Feb 2022 13:32:16 +0900 (JST)
Subject: [ruby-changes:71185] 1ae630db26 (master): `wmap#each` should check liveness of keys
https://git.ruby-lang.org/ruby.git/commit/?id=1ae630db26 From 1ae630db2682831cc0f2d381ff46e7b8cd3c2174 Mon Sep 17 00:00:00 2001 From: Koichi Sasada <ko1@a...> Date: Wed, 16 Feb 2022 11:07:45 +0900 Subject: `wmap#each` should check liveness of keys `ObjectSpace::WeakMap#each*` should check key's liveness. fix [Bug #18586] --- gc.c | 62 ++++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/gc.c b/gc.c index 1c80a5573f..457f6457c7 100644 --- a/gc.c +++ b/gc.c @@ -12428,15 +12428,24 @@ wmap_inspect(VALUE self) https://github.com/ruby/ruby/blob/trunk/gc.c#L12428 return str; } +static inline bool +wmap_live_entry_p(rb_objspace_t *objspace, st_data_t key, st_data_t val) +{ + return wmap_live_p(objspace, (VALUE)key) && wmap_live_p(objspace, (VALUE)val); +} + static int wmap_each_i(st_data_t key, st_data_t val, st_data_t arg) { rb_objspace_t *objspace = (rb_objspace_t *)arg; - VALUE obj = (VALUE)val; - if (wmap_live_p(objspace, obj)) { - rb_yield_values(2, (VALUE)key, obj); + + if (wmap_live_entry_p(objspace, key, val)) { + rb_yield_values(2, (VALUE)key, (VALUE)val); + return ST_CONTINUE; + } + else { + return ST_DELETE; } - return ST_CONTINUE; } /* Iterates over keys and objects in a weakly referenced object */ @@ -12455,11 +12464,14 @@ static int https://github.com/ruby/ruby/blob/trunk/gc.c#L12464 wmap_each_key_i(st_data_t key, st_data_t val, st_data_t arg) { rb_objspace_t *objspace = (rb_objspace_t *)arg; - VALUE obj = (VALUE)val; - if (wmap_live_p(objspace, obj)) { - rb_yield((VALUE)key); + + if (wmap_live_entry_p(objspace, key, val)) { + rb_yield((VALUE)key); + return ST_CONTINUE; + } + else { + return ST_DELETE; } - return ST_CONTINUE; } /* Iterates over keys and objects in a weakly referenced object */ @@ -12478,11 +12490,14 @@ static int https://github.com/ruby/ruby/blob/trunk/gc.c#L12490 wmap_each_value_i(st_data_t key, st_data_t val, st_data_t arg) { rb_objspace_t *objspace = (rb_objspace_t *)arg; - VALUE obj = (VALUE)val; - if (wmap_live_p(objspace, obj)) { - rb_yield(obj); + + if (wmap_live_entry_p(objspace, key, val)) { + rb_yield((VALUE)val); + return ST_CONTINUE; + } + else { + return ST_DELETE; } - return ST_CONTINUE; } /* Iterates over keys and objects in a weakly referenced object */ @@ -12503,11 +12518,14 @@ wmap_keys_i(st_data_t key, st_data_t val, st_data_t arg) https://github.com/ruby/ruby/blob/trunk/gc.c#L12518 struct wmap_iter_arg *argp = (struct wmap_iter_arg *)arg; rb_objspace_t *objspace = argp->objspace; VALUE ary = argp->value; - VALUE obj = (VALUE)val; - if (wmap_live_p(objspace, obj)) { - rb_ary_push(ary, (VALUE)key); + + if (wmap_live_entry_p(objspace, key, val)) { + rb_ary_push(ary, (VALUE)key); + return ST_CONTINUE; + } + else { + return ST_DELETE; } - return ST_CONTINUE; } /* Iterates over keys and objects in a weakly referenced object */ @@ -12530,11 +12548,14 @@ wmap_values_i(st_data_t key, st_data_t val, st_data_t arg) https://github.com/ruby/ruby/blob/trunk/gc.c#L12548 struct wmap_iter_arg *argp = (struct wmap_iter_arg *)arg; rb_objspace_t *objspace = argp->objspace; VALUE ary = argp->value; - VALUE obj = (VALUE)val; - if (wmap_live_p(objspace, obj)) { - rb_ary_push(ary, obj); + + if (wmap_live_entry_p(objspace, key, val)) { + rb_ary_push(ary, (VALUE)val); + return ST_CONTINUE; + } + else { + return ST_DELETE; } - return ST_CONTINUE; } /* Iterates over values and objects in a weakly referenced object */ @@ -12599,6 +12620,7 @@ wmap_lookup(VALUE self, VALUE key) https://github.com/ruby/ruby/blob/trunk/gc.c#L12620 VALUE obj; struct weakmap *w; rb_objspace_t *objspace = &rb_objspace; + GC_ASSERT(wmap_live_p(objspace, key)); TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w); if (!st_lookup(w->wmap2obj, (st_data_t)key, &data)) return Qundef; -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/