Skip to content

Fix GH-14361: Deep recursion in zend_cfg.c causes segfault instead of error #14432

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
wants to merge 3 commits into from
Closed
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
29 changes: 14 additions & 15 deletions Zend/Optimizer/zend_cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@ static void zend_mark_reachable(zend_op *opcodes, zend_cfg *cfg, zend_basic_bloc
{
zend_basic_block *blocks = cfg->blocks;

while (1) {
zend_worklist work;
ALLOCA_FLAG(list_use_heap)
ZEND_WORKLIST_ALLOCA(&work, cfg->blocks_count, list_use_heap);

zend_worklist_push(&work, b - cfg->blocks);

while (zend_worklist_len(&work)) {
int i;
b = cfg->blocks + zend_worklist_pop(&work);

b->flags |= ZEND_BB_REACHABLE;
if (b->successors_count == 0) {
b->flags |= ZEND_BB_EXIT;
return;
continue;
}

for (i = 0; i < b->successors_count; i++) {
Expand Down Expand Up @@ -86,22 +93,14 @@ static void zend_mark_reachable(zend_op *opcodes, zend_cfg *cfg, zend_basic_bloc
succ->flags |= ZEND_BB_FOLLOW;
}

if (i == b->successors_count - 1) {
/* Tail call optimization */
if (succ->flags & ZEND_BB_REACHABLE) {
return;
}

b = succ;
break;
} else {
/* Recursively check reachability */
if (!(succ->flags & ZEND_BB_REACHABLE)) {
zend_mark_reachable(opcodes, cfg, succ);
}
/* Check reachability of successor */
if (!(succ->flags & ZEND_BB_REACHABLE)) {
zend_worklist_push(&work, succ - cfg->blocks);
}
}
}

ZEND_WORKLIST_FREE_ALLOCA(&work, list_use_heap);
}
/* }}} */

Expand Down
Loading