Bugfix for use-after-free, as well as an issue with the order

grammars were loaded
This commit is contained in:
Bruce Hill 2021-10-01 19:29:31 -07:00
parent 73bbf6872a
commit 0ad64a1006
3 changed files with 6 additions and 4 deletions

2
bp.c
View File

@ -521,7 +521,7 @@ static int process_git_files(pat_t *pattern, int argc, char *argv[])
//
static pat_t *load_grammar(pat_t *defs, file_t *f)
{
return chain_together(defs, assert_pat(f->start, f->end, bp_pattern(f->start, f->end)));
return chain_together(assert_pat(f->start, f->end, bp_pattern(f->start, f->end)), defs);
}
//

View File

@ -5,7 +5,7 @@
# The grammar files provided with BP are not otherwise intended to be full
# language grammars.
Grammar: __ *(Def [__`;])%__ __ [@error=(+(./\n) => "Could not parse this code")]
Grammar: __ *(Def [__`;])%__ __ [@error=(+(./\n) => "Could not parse this code: @0")]
Def: @name=id __ `: __ (
@definition=extended-pat
/ $$ @error=(=>"No definition for rule")

View File

@ -636,7 +636,8 @@ static match_t *match(match_ctx_t *ctx, const char *str, pat_t *pat)
while (rec_op.args.leftrec.visits > 0) {
rec_op.args.leftrec.visits = 0;
recycle_match(&rec_op.args.leftrec.match);
if (rec_op.args.leftrec.match && rec_op.args.leftrec.match != m)
recycle_match(&rec_op.args.leftrec.match);
rec_op.args.leftrec.match = m;
prev = m->end;
match_t *m2 = match(&ctx2, str, ref);
@ -645,10 +646,11 @@ static match_t *match(match_ctx_t *ctx, const char *str, pat_t *pat)
recycle_match(&m2);
break;
}
recycle_match(&m);
m = m2;
}
if (rec_op.args.leftrec.match)
if (rec_op.args.leftrec.match && rec_op.args.leftrec.match != m)
recycle_match(&rec_op.args.leftrec.match);
// This match wrapper mainly exists for record-keeping purposes.