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

Skip to content

Make exception raised in ensure not caught by rescue prior to ensure #4291

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

Closed
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
26 changes: 26 additions & 0 deletions compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2522,6 +2522,8 @@ iseq_set_exception_table(rb_iseq_t *iseq)
if (tlen > 0) {
struct iseq_catch_table *table = xmalloc(iseq_catch_table_bytes(tlen));
table->size = tlen;
struct iseq_catch_table_entry *rescue_entry = NULL;
bool multiple_rescue = false;

for (i = 0; i < table->size; i++) {
ptr = RARRAY_CONST_PTR_TRANSIENT(tptr[i]);
Expand All @@ -2532,6 +2534,30 @@ iseq_set_exception_table(rb_iseq_t *iseq)
entry->iseq = (rb_iseq_t *)ptr[3];
RB_OBJ_WRITTEN(iseq, Qundef, entry->iseq);

/* TODO: Dirty Hack! Fix me */
/* This checks when a rescue catch entry and ensure catch
* entry have the same start, the rescue catch end is not
* after the end of the ensure end. This fixes an issue
* where a control flow keyword such as next inside a
* begin block results in the rescue catch entry covering
* the ensure block.
*/
if (entry->type == CATCH_TYPE_RESCUE) {
if (rescue_entry) {
multiple_rescue = true;
}
else {
rescue_entry = entry;
}
}
else if (entry->type == CATCH_TYPE_ENSURE &&
rescue_entry &&
!multiple_rescue &&
entry->start == rescue_entry->start &&
rescue_entry->end > entry->end) {
rescue_entry->end = entry->end;
}

/* stack depth */
if (ptr[4]) {
LABEL *lobj = (LABEL *)(ptr[4] & ~1);
Expand Down
29 changes: 29 additions & 0 deletions test/ruby/test_exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,35 @@ def test_exception_ensure_2 # just duplication?
assert(!bad)
end

def test_exception_in_ensure_with_next
string = "[ruby-core:82936] [Bug #13930]"
assert_raise_with_message(RuntimeError, string) do
lambda do
next
rescue
assert(false)
ensure
raise string
end.call
assert(false)
end
end

def test_exception_in_ensure_with_return
@string = "[ruby-core:97104] [Bug #16618]"
def self.meow
return
assert(false)
rescue
assert(false)
ensure
raise @string
end
assert_raise_with_message(RuntimeError, @string) do
meow
end
end

def test_errinfo_in_debug
bug9568 = EnvUtil.labeled_class("[ruby-core:61091] [Bug #9568]", RuntimeError) do
def to_s
Expand Down