Skip to content

YJIT: Fix String#setbyte crashing for converted arguments #10575

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 2 commits into from
Apr 22, 2024
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
17 changes: 17 additions & 0 deletions bootstraptest/test_yjit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4786,3 +4786,20 @@ def tests

tests
}

# test String#stebyte with arguments that need conversion
assert_equal "abc", %q{
str = +"a00"
def change_bytes(str, one, two)
str.setbyte(one, "b".ord)
str.setbyte(2, two)
end

to_int_1 = Object.new
to_int_99 = Object.new
def to_int_1.to_int = 1
def to_int_99.to_int = 99

change_bytes(str, to_int_1, to_int_99)
str
}
9 changes: 8 additions & 1 deletion object.c
Original file line number Diff line number Diff line change
Expand Up @@ -3236,15 +3236,22 @@ ALWAYS_INLINE(static VALUE rb_to_integer_with_id_exception(VALUE val, const char
static inline VALUE
rb_to_integer_with_id_exception(VALUE val, const char *method, ID mid, int raise)
{
// We need to pop the lazily pushed frame when not raising an exception.
rb_control_frame_t *current_cfp;
VALUE v;

if (RB_INTEGER_TYPE_P(val)) return val;
current_cfp = GET_EC()->cfp;
rb_yjit_lazy_push_frame(GET_EC()->cfp->pc);
v = try_to_int(val, mid, raise);
if (!raise && NIL_P(v)) return Qnil;
if (!raise && NIL_P(v)) {
GET_EC()->cfp = current_cfp;
return Qnil;
}
if (!RB_INTEGER_TYPE_P(v)) {
conversion_mismatch(val, "Integer", method, v);
}
GET_EC()->cfp = current_cfp;
return v;
}
#define rb_to_integer(val, method, mid) \
Expand Down