From 6d0e4fd1d2bf69b2c36fbda72ebb6b427348318d Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 7 Jun 2024 13:30:25 -0400 Subject: Bugfix: upto patterns should not accept an empty string as a target, which fixes an issue where `echo "hello@" | bp '{..}{"@"}'` would fail to match properly. --- pattern.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'pattern.c') diff --git a/pattern.c b/pattern.c index 2a9767d..a3d66e6 100644 --- a/pattern.c +++ b/pattern.c @@ -160,6 +160,9 @@ public bp_pat_t *chain_together(bp_pat_t *first, bp_pat_t *second) if (first == NULL) return second; if (second == NULL) return first; + if (first->type == BP_STRING && first->max_matchlen == 0) return second; + if (second->type == BP_STRING && second->max_matchlen == 0) return first; + if (first->type == BP_DEFINITIONS && second->type == BP_DEFINITIONS) { return Pattern(BP_CHAIN, first->start, second->end, second->min_matchlen, second->max_matchlen, .first=first, .second=second); } @@ -244,6 +247,10 @@ static bp_pat_t *_bp_simplepattern(const char *str, const char *end, bool inside target = NULL; } else { target = bp_simplepattern(str, end); + // Bugfix: `echo "foo@" | bp '{..}{"@"}'` should be parsed as `.."@"` + // not '(.."") "@"' + while (target && target->type == BP_STRING && target->max_matchlen == 0) + target = bp_simplepattern(target->end, end); } return type == BP_UPTO ? Pattern(BP_UPTO, start, str, 0, -1, .target=target, .skip=extra_arg) -- cgit v1.2.3