merge revision(s) 056497319658cbefe22351c6ec5c9fa6e4df72bd: [Backport #21357]

[Bug #21357] Fix crash in Hash#merge with block

	Prior to 49b306ecb9
	the `optional_arg` passed from `rb_hash_update_block_i` to `tbl_update`
	was a hash value (i.e. a VALUE). After that commit it changed to an
	`update_call_args`.

	If the block sets or changes the value, `tbl_update_modify` will set the
	`arg.value` back to an actual value and we won't crash. But in the case
	where the block returns the original value we end up calling
	`RB_OBJ_WRITTEN` with the `update_call_args` which is not expected and
	may crash.

	`arg.value` appears to only be used to pass to `RB_OBJ_WRITTEN` (others
	who need the `update_call_args` get it from `arg.arg`), so I don't think
	it needs to be set to anything upfront. And `tbl_update_modify` will set
	the `arg.value` in the cases we need the write barrier.
This commit is contained in:
nagachika 2025-05-24 10:25:59 +09:00
parent 8f895758d9
commit 24c994b91a
3 changed files with 8 additions and 3 deletions

4
hash.c
View File

@ -1710,14 +1710,14 @@ tbl_update(VALUE hash, VALUE key, tbl_update_func func, st_data_t optional_arg)
.func = func,
.hash = hash,
.key = key,
.value = (VALUE)optional_arg,
.value = 0
};
int ret = rb_hash_stlike_update(hash, key, tbl_update_modify, (st_data_t)&arg);
/* write barrier */
RB_OBJ_WRITTEN(hash, Qundef, arg.key);
RB_OBJ_WRITTEN(hash, Qundef, arg.value);
if (arg.value) RB_OBJ_WRITTEN(hash, Qundef, arg.value);
return ret;
}

View File

@ -2315,6 +2315,11 @@ class TestHashOnly < Test::Unit::TestCase
end
end
def test_bug_21357
h = {x: []}.merge(x: nil) { |_k, v1, _v2| v1 }
assert_equal({x: []}, h)
end
def test_any_hash_fixable
20.times do
assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")

View File

@ -11,7 +11,7 @@
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
#define RUBY_VERSION_TEENY 8
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
#define RUBY_PATCHLEVEL 154
#define RUBY_PATCHLEVEL 155
#include "ruby/version.h"
#include "ruby/internal/abi.h"