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

parser: Avoid reading after the end of shunting yard stack #209

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 7 additions & 4 deletions parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,10 @@ static bool parser_sy_apply_operator(parser_t *parser, shunt *sy)
if (!op->operands)
return true;

sy->out.erase(sy->out.end() - op->operands, sy->out.end());
size_t start_offset = sy->out.size() - op->operands;
for (i = 0; i < op->operands; ++i) {
exprs[i] = sy->out[sy->out.size()+i].out;
blocks[i] = sy->out[sy->out.size()+i].block;
exprs[i] = sy->out[start_offset+i].out;
blocks[i] = sy->out[start_offset+i].block;

if (exprs[i]->m_vtype == TYPE_NOEXPR &&
!(i != 0 && op->id == opid2('?',':')) &&
Expand All @@ -393,6 +393,7 @@ static bool parser_sy_apply_operator(parser_t *parser, shunt *sy)
(void)!compile_warning(exprs[i]->m_context, WARN_DEBUG, "expression %u\n", (unsigned int)i);
}
}
sy->out.erase(sy->out.end() - op->operands, sy->out.end());

if (blocks[0] && blocks[0]->m_exprs.empty() && op->id != opid1(',')) {
compile_error(ctx, "internal error: operator cannot be applied on empty blocks");
Expand Down Expand Up @@ -1205,8 +1206,10 @@ static bool parser_close_call(parser_t *parser, shunt *sy)
size_t fid;
size_t paramcount, i;
bool fold = true;
lex_ctx_t ctx;

fid = sy->ops.back().off;
ctx = sy->ops.back().ctx;
sy->ops.pop_back();

/* out[fid] is the function
Expand Down Expand Up @@ -1292,7 +1295,7 @@ static bool parser_close_call(parser_t *parser, shunt *sy)
}

fold_leave:
call = ast_call::make(sy->ops[sy->ops.size()].ctx, fun);
call = ast_call::make(ctx, fun);

if (!call)
return false;
Expand Down