ruby-changes:56369
From: Nobuyoshi <ko1@a...>
Date: Thu, 4 Jul 2019 18:59:08 +0900 (JST)
Subject: [ruby-changes:56369] Nobuyoshi Nakada: 265b9a0edf (master): Parse key sequence more strictly
https://git.ruby-lang.org/ruby.git/commit/?id=265b9a0edf From 265b9a0edf48e96d053724c9676af953e920246c Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Thu, 4 Jul 2019 18:54:50 +0900 Subject: Parse key sequence more strictly diff --git a/lib/reline/config.rb b/lib/reline/config.rb index 5c10d7f..09e5725 100644 --- a/lib/reline/config.rb +++ b/lib/reline/config.rb @@ -5,6 +5,8 @@ class Reline::Config https://github.com/ruby/ruby/blob/trunk/lib/reline/config.rb#L5 DEFAULT_PATH = '~/.inputrc' + KEYSEQ_PATTERN = /\\C-[A-Za-z_]|\\M-[0-9A-Za-z_]|\\C-M-[A-Za-z_]|\\M-C-[A-Za-z_]|\\e|\\[\\\"\'abdfnrtv]|\\\d{1,3}|\\x\h{1,2}|./ + class InvalidInputrc < RuntimeError attr_accessor :file, :lineno end @@ -137,9 +139,10 @@ class Reline::Config https://github.com/ruby/ruby/blob/trunk/lib/reline/config.rb#L139 next end - if line =~ /\s*(.*)\s*:\s*(.*)\s*$/ + if line =~ /\s*("#{KEYSEQ_PATTERN}+")\s*:\s*(.*)\s*$/ key, func_name = $1, $2 keystroke, func = bind_key(key, func_name) + next unless keystroke @additional_key_bindings[keystroke] = func end end @@ -227,7 +230,7 @@ class Reline::Config https://github.com/ruby/ruby/blob/trunk/lib/reline/config.rb#L230 end def bind_key(key, func_name) - if key =~ /"(.*)"/ + if key =~ /\A"(.*)"\z/ keyseq = parse_keyseq($1) else keyseq = nil @@ -277,9 +280,8 @@ class Reline::Config https://github.com/ruby/ruby/blob/trunk/lib/reline/config.rb#L280 def parse_keyseq(str) # TODO: Control- and Meta- ret = [] - while str =~ /(\\C-[A-Za-z_]|\\M-[0-9A-Za-z_]|\\C-M-[A-Za-z_]|\\M-C-[A-Za-z_]|\\e|\\\\|\\"|\\'|\\a|\\b|\\d|\\f|\\n|\\r|\\t|\\v|\\\d{1,3}|\\x\h{1,2}|.)/ + str.scan(KEYSEQ_PATTERN) do ret << key_notation_to_code($&) - str = $' end ret end diff --git a/test/reline/test_config.rb b/test/reline/test_config.rb index ee006b7..58f265d 100644 --- a/test/reline/test_config.rb +++ b/test/reline/test_config.rb @@ -33,6 +33,11 @@ class Reline::Config::Test < Reline::TestCase https://github.com/ruby/ruby/blob/trunk/test/reline/test_config.rb#L33 assert_not_include @config.key_bindings, nil end + def test_invalid_keystroke + @config.read_lines(["a: error\n"]) + assert_not_include @config.key_bindings, nil + end + def test_bind_key assert_equal ['input'.bytes, 'abcde'.bytes], @config.bind_key('"input"', '"abcde"') end -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/