From 2db7945d6f6672648f56dfad1b5cab990c3aa562 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Thu, 20 May 2021 21:57:22 -0500 Subject: [PATCH] Fix peg bug when there is no default grammar set. This could result in a segfault when we attempt to read from a NULL pointer. --- src/core/peg.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/core/peg.c b/src/core/peg.c index 49b8088e..eb2a2265 100644 --- a/src/core/peg.c +++ b/src/core/peg.c @@ -1148,7 +1148,9 @@ static uint32_t peg_compile1(Builder *b, Janet peg) { for (; i > 0 && janet_checktype(peg, JANET_KEYWORD); --i) { Janet nextPeg = janet_table_get_ex(grammar, peg, &grammar); if (!grammar || janet_checktype(nextPeg, JANET_NIL)) { - nextPeg = janet_table_get(b->default_grammar, peg); + nextPeg = (b->default_grammar == NULL) + ? janet_wrap_nil() + : janet_table_get(b->default_grammar, peg); if (janet_checktype(nextPeg, JANET_NIL)) { peg_panic(b, "unknown rule"); }