From 39c6be7cb71e717bb5ff83dd8cd567bb2aec29ac Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Thu, 4 Nov 2021 19:38:37 -0500 Subject: [PATCH] Fix #861 - parser/produce caused state to be invalid for parser/state. parser/produce was leaving a counter in the root state undecremented. --- src/core/parse.c | 6 +++++- test/suite0010.janet | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/core/parse.c b/src/core/parse.c index c8e92e07..60212deb 100644 --- a/src/core/parse.c +++ b/src/core/parse.c @@ -746,6 +746,7 @@ Janet janet_parser_produce(JanetParser *parser) { } parser->pending--; parser->argcount--; + parser->states[0].argn--; return ret; } @@ -759,6 +760,7 @@ Janet janet_parser_produce_wrapped(JanetParser *parser) { } parser->pending--; parser->argcount--; + parser->states[0].argn--; return ret; } @@ -1194,7 +1196,9 @@ static Janet parser_state_frames(const JanetParser *p) { Janet *args = p->args + p->argcount; for (int32_t i = count - 1; i >= 0; --i) { JanetParseState *s = p->states + i; - args -= s->argn; + if (s->flags & PFLAG_CONTAINER) { + args -= s->argn; + } states->data[i] = janet_wrap_parse_state(s, args, buf, (uint32_t) p->bufcount); } return janet_wrap_array(states); diff --git a/test/suite0010.janet b/test/suite0010.janet index 84913d51..2550361c 100644 --- a/test/suite0010.janet +++ b/test/suite0010.janet @@ -199,4 +199,18 @@ (def stream-env (dofile r)) (assert (= (stream-env :x) 10) "dofile stream 1") +# Issue #861 - should be valgrind clean +(def step1 "(a b c d)\n") +(def step2 "(a b)\n") +(def p1 (parser/new)) +(pp (parser/state p1)) +(parser/consume p1 step1) +(loop [v :iterate (parser/produce p1)]) +(parser/state p1) +(def p2 (parser/clone p1)) +(parser/state p2) +(parser/consume p2 step2) +(loop [v :iterate (parser/produce p2)]) +(parser/state p2) + (end-suite)