diff --git a/lib/reline/config.rb b/lib/reline/config.rb index e0fc37fc68..d437156650 100644 --- a/lib/reline/config.rb +++ b/lib/reline/config.rb @@ -177,6 +177,11 @@ def read_lines(lines, file = nil) if_stack = [] lines.each_with_index do |line, no| + # Even after encoding conversion, we need to verify the encoding is valid + # as some invalid byte sequences might pass through the conversion. + unless line.valid_encoding? + raise InvalidInputrc, "#{file}:#{no + 1}: can't be converted to the locale #{Reline.encoding_system_needs.name}" + end next if line.match(/\A\s*#/) no += 1 diff --git a/test/reline/test_config.rb b/test/reline/test_config.rb index 3c9094eece..ff3e121ab0 100644 --- a/test/reline/test_config.rb +++ b/test/reline/test_config.rb @@ -613,4 +613,21 @@ def test_reload @config.reload assert_equal '@', @config.emacs_mode_string end + + def test_invalid_byte_sequence_inputrc + lines = [ + "set vi-cmd-mode-string\n", + "$if Ruby\n", + " \"\C-a\": \"Ruby\"\n", + "$else \"\xFF\"\n".dup.force_encoding(Reline.encoding_system_needs), # Invalid byte sequence + " \"\C-b\": \"NotRuby\"\n", + "$endif\n" + ] + + e = assert_raise(Reline::Config::InvalidInputrc) do + @config.read_lines(lines, "INPUTRC") + end + + assert_equal "INPUTRC:4: can't be converted to the locale #{Reline.encoding_system_needs}", e.message + end end