From 225d3bb87e10a71c9dd6058e2ac2cc389400e2dc Mon Sep 17 00:00:00 2001 From: Tom Fleet Date: Fri, 2 Feb 2024 21:12:52 +0000 Subject: [PATCH] Better fix for TakeWhileBetween fuzz failure --- parser.go | 13 +++++-------- parser_test.go | 4 ++-- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/parser.go b/parser.go index 42cf6dc..7570143 100644 --- a/parser.go +++ b/parser.go @@ -288,7 +288,7 @@ func TakeWhileBetween(lower, upper int, predicate func(r rune) bool) Parser[stri return "", "", errors.New("TakeWhileBetween: predicate never returned true") } - index := -1 // Index of last char for which predicate returns true + index := 0 // Index of last char for which predicate returns true for pos, char := range input { if !predicate(char) { break @@ -296,13 +296,10 @@ func TakeWhileBetween(lower, upper int, predicate func(r rune) bool) Parser[stri // Add the byte width of the char in question because the next char is the // first one where predicate(char) == false, that's where we want to cut // the string - index = pos + utf8.RuneLen(char) - } - - // This is *extremely* rare and I only found it with fuzz testing! I'm still not entirely - // sure why this can happen, but this is a best stab at explaining the error - if index == -1 { - return "", "", errors.New("TakeWhileBetween: invalid utf-8 byte split") + runeLen := utf8.RuneLen(char) + if runeLen != -1 { + index = pos + utf8.RuneLen(char) + } } // If we have an index, our job now is to return whichever is longest out of diff --git a/parser_test.go b/parser_test.go index 824e72a..de06656 100644 --- a/parser_test.go +++ b/parser_test.go @@ -687,7 +687,7 @@ func TestTakeWhileBetween(t *testing.T) { err: "", }, { - name: "fuzz failure", + name: "fuzz failure", // Now correctly handled! input: "\U0001925e0", lower: 9, upper: 83, @@ -695,7 +695,7 @@ func TestTakeWhileBetween(t *testing.T) { value: "", remainder: "", wantErr: true, - err: "TakeWhileBetween: invalid utf-8 byte split", + err: "TakeWhileBetween: predicate matched only 0 chars (), below lower limit (9)", }, { name: "nil predicate",