Skip to content
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

[PRISM] Fix StackOverflow for IfNode with logical predicate #9768

Merged
merged 2 commits into from
Jan 30, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 6 additions & 11 deletions prism_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,9 @@ pm_compile_logical(rb_iseq_t *iseq, LINK_ANCHOR *const ret, pm_node_t *cond,
return;
}
if (!label->refcnt) {
PM_PUTNIL;
if (popped) {
PM_PUTNIL;
}
}
else {
ADD_LABEL(seq, label);
Expand Down Expand Up @@ -549,6 +551,7 @@ pm_compile_flip_flop(pm_flip_flop_node_t *flip_flop_node, LABEL *else_label, LAB
}

void pm_compile_defined_expr(rb_iseq_t *iseq, const pm_node_t *defined_node, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node, NODE dummy_line_node, int lineno, bool in_condition);

static void
pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const pm_node_t *cond,
LABEL *then_label, LABEL *else_label, const uint8_t *src, bool popped, pm_scope_node_t *scope_node)
Expand Down Expand Up @@ -597,11 +600,7 @@ pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const pm_no
break;
}
default: {
DECL_ANCHOR(cond_seq);
INIT_ANCHOR(cond_seq);

pm_compile_node(iseq, cond, cond_seq, src, false, scope_node);
ADD_SEQ(ret, cond_seq);
pm_compile_node(iseq, cond, ret, src, false, scope_node);
break;
}
}
Expand All @@ -615,17 +614,13 @@ pm_compile_if(rb_iseq_t *iseq, const int line, pm_statements_node_t *node_body,
{
NODE dummy_line_node = generate_dummy_line_node(line, line);

DECL_ANCHOR(cond_seq);

LABEL *then_label, *else_label, *end_label;

INIT_ANCHOR(cond_seq);
then_label = NEW_LABEL(line);
else_label = NEW_LABEL(line);
end_label = 0;

pm_compile_branch_condition(iseq, cond_seq, predicate, then_label, else_label, src, false, scope_node);
ADD_SEQ(ret, cond_seq);
pm_compile_branch_condition(iseq, ret, predicate, then_label, else_label, src, false, scope_node);

if (then_label->refcnt) {
ADD_LABEL(ret, then_label);
Expand Down
1 change: 1 addition & 0 deletions test/ruby/test_compile_prism.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,7 @@ def test_IfNode
assert_prism_eval('if ..1; end')
assert_prism_eval('if 1..; end')
assert_prism_eval('if 1..2; end')
assert_prism_eval('if true or true; end');
end

def test_OrNode
Expand Down