Skip to content

Commit

Permalink
merge revision(s) a2d4e1c,d6c5a30cfdf658280338dbb8c8b17fab3190b928: […
Browse files Browse the repository at this point in the history
…Backport #18392]

	Fixed the check order in wmap_live_p [Bug #18392]

	Check if the object is a pointer to heap before check the flag in
	that object.
	---
	 gc.c | 35 ++++++++++++++++++++++-------------
	 1 file changed, 22 insertions(+), 13 deletions(-)

	ObjectSpace::WeakMap#inspect: check if living object [Bug #18392]

	---
	 gc.c                      | 29 +++++++++++++++++++++++------
	 test/ruby/test_weakmap.rb |  9 +++++++++
	 2 files changed, 32 insertions(+), 6 deletions(-)
  • Loading branch information
unak committed Dec 31, 2021
1 parent b829811 commit 7d3cff6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
29 changes: 23 additions & 6 deletions gc.c
Expand Up @@ -10516,10 +10516,26 @@ struct wmap_iter_arg {
VALUE value;
};

static VALUE
wmap_inspect_append(rb_objspace_t *objspace, VALUE str, VALUE obj)
{
if (SPECIAL_CONST_P(obj)) {
return rb_str_append(str, rb_inspect(obj));
}
else if (wmap_live_p(objspace, obj)) {
return rb_str_append(str, rb_any_to_s(obj));
}
else {
return rb_str_catf(str, "#<collected:%p>", (void*)obj);
}
}

static int
wmap_inspect_i(st_data_t key, st_data_t val, st_data_t arg)
{
VALUE str = (VALUE)arg;
struct wmap_iter_arg *argp = (struct wmap_iter_arg *)arg;
rb_objspace_t *objspace = argp->objspace;
VALUE str = argp->value;
VALUE k = (VALUE)key, v = (VALUE)val;

if (RSTRING_PTR(str)[0] == '#') {
Expand All @@ -10529,11 +10545,9 @@ wmap_inspect_i(st_data_t key, st_data_t val, st_data_t arg)
rb_str_cat2(str, ": ");
RSTRING_PTR(str)[0] = '#';
}
k = SPECIAL_CONST_P(k) ? rb_inspect(k) : rb_any_to_s(k);
rb_str_append(str, k);
wmap_inspect_append(objspace, str, k);
rb_str_cat2(str, " => ");
v = SPECIAL_CONST_P(v) ? rb_inspect(v) : rb_any_to_s(v);
rb_str_append(str, v);
wmap_inspect_append(objspace, str, v);

return ST_CONTINUE;
}
Expand All @@ -10544,11 +10558,14 @@ wmap_inspect(VALUE self)
VALUE str;
VALUE c = rb_class_name(CLASS_OF(self));
struct weakmap *w;
struct wmap_iter_arg args;

TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
str = rb_sprintf("-<%"PRIsVALUE":%p", c, (void *)self);
if (w->wmap2obj) {
st_foreach(w->wmap2obj, wmap_inspect_i, str);
args.objspace = &rb_objspace;
args.value = str;
st_foreach(w->wmap2obj, wmap_inspect_i, (st_data_t)&args);
}
RSTRING_PTR(str)[0] = '#';
rb_str_cat2(str, ">");
Expand Down
9 changes: 9 additions & 0 deletions test/ruby/test_weakmap.rb
Expand Up @@ -73,6 +73,15 @@ def test_inspect
@wm.inspect)
end

def test_inspect_garbage
1000.times do |i|
@wm[i] = Object.new
@wm.inspect
end
assert_match(/\A\#<#{@wm.class.name}:[^:]++:(?:\s\d+\s=>\s\#<(?:Object|collected):[^:<>]*+>(?:,|>\z))+/,
@wm.inspect)
end

def test_each
m = __callee__[/test_(.*)/, 1]
x1 = Object.new
Expand Down
2 changes: 1 addition & 1 deletion version.h
Expand Up @@ -2,7 +2,7 @@
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
#define RUBY_VERSION_TEENY 6
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
#define RUBY_PATCHLEVEL 205
#define RUBY_PATCHLEVEL 206

#define RUBY_RELEASE_YEAR 2021
#define RUBY_RELEASE_MONTH 12
Expand Down

0 comments on commit 7d3cff6

Please sign in to comment.