Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug #21331] Prohibit modification during stlike loop #13317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 66 additions & 8 deletions hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ ar_general_foreach(VALUE hash, st_foreach_check_callback_func *func, st_update_c
return 0;
case ST_REPLACE:
if (replace) {
retval = (*replace)(&key, &val, arg, TRUE);
(*replace)(&key, &val, arg, TRUE);

// TODO: pair should be same as pair before.
pair = RHASH_AR_TABLE_REF(hash, i);
Expand Down Expand Up @@ -1404,26 +1404,84 @@ hash_foreach_ensure(VALUE hash)
return 0;
}

int
rb_hash_stlike_foreach(VALUE hash, st_foreach_callback_func *func, st_data_t arg)
struct hash_stlike_foreach_arg {
VALUE hash;
st_foreach_callback_func *func;
VALUE arg;
};

static VALUE
hash_stlike_foreach_call(VALUE args)
{
struct hash_stlike_foreach_arg *argp = (void *)args;
VALUE hash = argp->hash;
st_foreach_callback_func *func = argp->func;
VALUE arg = argp->arg;
int ret;

if (RHASH_AR_TABLE_P(hash)) {
return ar_foreach(hash, func, arg);
ret = ar_foreach(hash, func, arg);
}
else {
return st_foreach(RHASH_ST_TABLE(hash), func, arg);
ret = st_foreach(RHASH_ST_TABLE(hash), func, arg);
}
return (VALUE)ret;
}

int
rb_hash_stlike_foreach_with_replace(VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
rb_hash_stlike_foreach(VALUE hash, st_foreach_callback_func *func, st_data_t arg)
{
struct hash_stlike_foreach_arg args = {
.hash = hash,
.func = func,
.arg = arg,
};
hash_iter_lev_inc(hash);
VALUE ret = rb_ensure(hash_stlike_foreach_call, (VALUE)&args,
hash_foreach_ensure, hash);
return (int)ret;
}

struct hash_stlike_foreach_with_replace_arg {
VALUE hash;
st_foreach_check_callback_func *func;
st_update_callback_func *replace;
VALUE arg;
};

static VALUE
hash_stlike_foreach_with_replace_call(VALUE args)
{
struct hash_stlike_foreach_with_replace_arg *argp = (void *)args;
VALUE hash = argp->hash;
st_foreach_check_callback_func *func = argp->func;
st_update_callback_func *replace = argp->replace;
VALUE arg = argp->arg;
int ret;

if (RHASH_AR_TABLE_P(hash)) {
return ar_foreach_with_replace(hash, func, replace, arg);
ret = ar_foreach_with_replace(hash, func, replace, arg);
}
else {
return st_foreach_with_replace(RHASH_ST_TABLE(hash), func, replace, arg);
ret = st_foreach_with_replace(RHASH_ST_TABLE(hash), func, replace, arg);
}
return (VALUE)ret;
}

int
rb_hash_stlike_foreach_with_replace(VALUE hash, st_foreach_check_callback_func *func,
st_update_callback_func *replace, st_data_t arg)
{
struct hash_stlike_foreach_with_replace_arg args = {
.hash = hash,
.func = func,
.replace = replace,
.arg = arg,
};
hash_iter_lev_inc(hash);
VALUE ret = rb_ensure(hash_stlike_foreach_with_replace_call, (VALUE)&args,
hash_foreach_ensure, hash);
return (int)ret;
}

static VALUE
Expand Down
8 changes: 8 additions & 0 deletions test/ruby/test_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1853,6 +1853,14 @@ def test_transform_values_bang
end
end
assert_equal(@cls[a: 2, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10], x)

x = (1..1337).to_h {|k| [k, k]}
assert_raise_with_message(RuntimeError, /rehash during iteration/) do
x.transform_values! {|v|
x.rehash if v == 1337
v * 2
}
end
end

def hrec h, n, &b
Expand Down
Loading