diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-04-15 01:07:14 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-04-15 01:07:14 +0000 |
commit | 9559448bf5d7e9cfe13a5f7155f676e9c68be26d (patch) | |
tree | 8ddbf0a60c7c7720267866a557d7babd39292d7d | |
parent | 56d62aa6eeff1a0f95ec0b5f7a903b75bfd9f310 (diff) | |
download | ruby-9559448bf5d7e9cfe13a5f7155f676e9c68be26d.tar.gz |
io/console: unknown key code for names with nul
* ext/io/console/console.c (console_key_pressed_p): raise the same
exception, "unknown virtual key code", for names with nul chars.
though console_win32_vk() considers the length and can deal with
nul chars, rb_sprintf() raised at PRIsVALUE previously, so quote
it if it is unprintable.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54589 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 8 | ||||
-rw-r--r-- | ext/io/console/console.c | 7 | ||||
-rw-r--r-- | test/io/console/test_io_console.rb | 15 |
3 files changed, 28 insertions, 2 deletions
@@ -1,3 +1,11 @@ +Fri Apr 15 10:07:11 2016 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * ext/io/console/console.c (console_key_pressed_p): raise the same + exception, "unknown virtual key code", for names with nul chars. + though console_win32_vk() considers the length and can deal with + nul chars, rb_sprintf() raised at PRIsVALUE previously, so quote + it if it is unprintable. + Fri Apr 15 09:02:58 2016 Nobuyoshi Nakada <nobu@ruby-lang.org> * ext/io/console/console.c (rb_sym2str): fallback definition for diff --git a/ext/io/console/console.c b/ext/io/console/console.c index ec7ecdafe7..dbbfbb7463 100644 --- a/ext/io/console/console.c +++ b/ext/io/console/console.c @@ -731,11 +731,14 @@ console_key_pressed_p(VALUE io, VALUE k) const char *kn; if (SYMBOL_P(k)) { k = rb_sym2str(k); + kn = RSTRING_PTR(k); + } + else { + kn = StringValuePtr(k); } - kn = StringValueCStr(k); t = console_win32_vk(kn, RSTRING_LEN(k)); if (!t || (vk = (short)t->vk) == -1) { - rb_raise(rb_eArgError, "unknown virtual key code: %"PRIsVALUE, k); + rb_raise(rb_eArgError, "unknown virtual key code: % "PRIsVALUE, k); } } return GetKeyState(vk) & 0x80 ? Qtrue : Qfalse; diff --git a/test/io/console/test_io_console.rb b/test/io/console/test_io_console.rb index 4876843555..ff705f7d7c 100644 --- a/test/io/console/test_io_console.rb +++ b/test/io/console/test_io_console.rb @@ -340,6 +340,21 @@ defined?(IO.console) and TestIO_Console.class_eval do end end +defined?(IO.console) and IO.console and IO.console.respond_to?(:pressed?) and + TestIO_Console.class_eval do + def test_pressed_valid + assert_include([true, false], IO.console.pressed?("HOME")) + assert_include([true, false], IO.console.pressed?(:"HOME")) + end + + def test_pressed_invalid + e = assert_raise(ArgumentError) do + IO.console.pressed?("HOME\0") + end + assert_match(/unknown virtual key code/, e.message) + end +end + TestIO_Console.class_eval do def test_stringio_getch assert_separately %w"--disable=gems -rstringio -rio/console", %q{ |