Skip to content

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

Merged
mame merged 1 commit intoruby:masterfrom
composerinteralia:bug-21357
May 22, 2025
Merged

[Bug #21357] Fix crash in Hash#merge with block#13404
mame merged 1 commit intoruby:masterfrom
composerinteralia:bug-21357

Conversation

@composerinteralia
Copy link
Contributor

@composerinteralia composerinteralia commented May 22, 2025

Prior to 49b306e 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.

Bug #21357

@composerinteralia composerinteralia changed the title [Bug #213557] Fix crash in Hash#merge with block [Bug #21357] Fix crash in Hash#merge with block May 22, 2025
Prior to ruby@49b306e
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.
Copy link
Member

@mame mame left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have just written the same patch. I lost. Thanks!

@mame mame enabled auto-merge (rebase) May 22, 2025 03:06
@mame mame merged commit 0564973 into ruby:master May 22, 2025
82 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants