make peg check more paranoid, check at runtime too

This commit is contained in:
Calvin Rose committed 2026-10-29 18:18:39 -05:00
1 parent 6f49ba66af
commit cdcf00f35c
2 files changed
+3 -4

No files matched your search

+1 -4
View File
@@ -426,7 +426,7 @@ tail:
case RULE_ARGUMENT: {
int32_t index = ((int32_t *)rule)[1];
Janet capture = (index >= s->extrac) ? janet_wrap_nil() : s->extrav[index];
Janet capture = (index >= s->extrac || index < 0) ? janet_wrap_nil() : s->extrav[index];
pushcap(s, capture, rule[2]);
return text;
}
@@ -1764,9 +1764,6 @@ static void *peg_unmarshal(JanetMarshalContext *ctx) {
case RULE_ARGUMENT:
/* [argument-index, tag] */
OVERFLOW_CHECK(3);
/* index is read as a signed int32 at match time and only
* bounds-checked against extrac on the high side, so a
* negative index would read before extrav */
if (((int32_t) rule[1]) < 0) goto bad;
i += 3;
break;
+2
View File
@@ -963,6 +963,8 @@
```))
(assert-error "negative argument" (peg/compile ~(argument -1)))
# A matchtime or replacement callback that grows the buffer being matched
# reallocates its storage. The matcher kept pointing at the old allocation,
# so the rest of the match read freed memory.