From 1bebd6f4f5fac7c98ad01696fd819c3c82e41ec6 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Fri, 18 Nov 2022 16:18:11 +0900 Subject: [PATCH] Fix panic on inverse match query with `--tiebreak=chunk` Fix #3055 --- CHANGELOG.md | 4 ++++ src/result.go | 23 ++++++++++++----------- test/test_go.rb | 4 ++++ 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56d460a..de3b9af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ CHANGELOG ========= +0.35.1 +------ +- Fixed a bug where fzf with `--tiebreak=chunk` crashes on inverse match query + 0.35.0 ------ - Added `start` event that is triggered only once when fzf finder starts. diff --git a/src/result.go b/src/result.go index c4acb5f..1212cab 100644 --- a/src/result.go +++ b/src/result.go @@ -50,20 +50,21 @@ func buildResult(item *Item, offsets []Offset, score int) Result { // Higher is better val = math.MaxUint16 - util.AsUint16(score) case byChunk: - b := minBegin - e := maxEnd - l := item.text.Length() - for ; b >= 1; b-- { - if unicode.IsSpace(item.text.Get(b - 1)) { - break + if validOffsetFound { + b := minBegin + e := maxEnd + for ; b >= 1; b-- { + if unicode.IsSpace(item.text.Get(b - 1)) { + break + } } - } - for ; e < l; e++ { - if unicode.IsSpace(item.text.Get(e)) { - break + for ; e < numChars; e++ { + if unicode.IsSpace(item.text.Get(e)) { + break + } } + val = util.AsUint16(e - b) } - val = util.AsUint16(e - b) case byLength: val = item.TrimLength() case byBegin, byEnd: diff --git a/test/test_go.rb b/test/test_go.rb index 41767fc..b7ae151 100755 --- a/test/test_go.rb +++ b/test/test_go.rb @@ -779,6 +779,10 @@ class TestGoFZF < TestBase '2 foobar baz', '3 foo barbaz' ], `#{FZF} -fba --tiebreak=chunk < #{tempname}`.lines(chomp: true) + + assert_equal [ + '3 foo barbaz' + ], `#{FZF} -f'!foobar' --tiebreak=chunk < #{tempname}`.lines(chomp: true) end def test_invalid_cache