diff --git a/fzf b/fzf index 2f90861..8cd2756 100755 --- a/fzf +++ b/fzf @@ -366,7 +366,8 @@ class FZF end if str end def addstr_safe str - C.addstr str.gsub("\0", '') + str = str.gsub("\0", '') rescue str + C.addstr str end def print_input @@ -1105,7 +1106,7 @@ class FZF begin unless @delim # AWK default - prefix_length = (str.index(/\S/) rescue 0)|| 0 + prefix_length = (str.index(/\S/) || 0) rescue 0 tokens = str.scan(/\S+\s*/) rescue [] else prefix_length = 0 diff --git a/test/test_fzf.rb b/test/test_fzf.rb index 4f9e6e1..806df8a 100644 --- a/test/test_fzf.rb +++ b/test/test_fzf.rb @@ -5,6 +5,7 @@ require 'curses' require 'timeout' require 'stringio' require 'minitest/autorun' +require 'tempfile' $LOAD_PATH.unshift File.expand_path('../..', __FILE__) ENV['FZF_EXECUTABLE'] = '0' load 'fzf' @@ -614,7 +615,7 @@ class TestFZF < MiniTest::Unit::TestCase def test_select_1_ambiguity stream = stream_for "Hello\nWorld" begin - Timeout::timeout(3) do + Timeout::timeout(2) do FZF.new(%w[--query=o --select-1], stream).start end flunk 'Should not reach here' @@ -681,5 +682,19 @@ class TestFZF < MiniTest::Unit::TestCase # **[***** #] => [******# ] assert_equal [true, 0, 6], fzf.constrain(2, 10, 7, 10) end + + def test_invalid_utf8 + tmp = Tempfile.new('fzf') + tmp << 'hello ' << [0xff].pack('C*') << ' world' << $/ << [0xff].pack('C*') + tmp.close + begin + Timeout::timeout(1) do + FZF.new(%w[-n..,1,2.. -q^ -x], File.open(tmp.path)).start + end + rescue Timeout::Error + end + ensure + tmp.unlink + end end