Bug #4217: irb exits unexpectedly with non-ascii Regexp on Windows
http://redmine.ruby-lang.org/issues/show/4217

Author: Heesob Park
Status: Open, Priority: Normal
Category: lib, Target version: 1.9.3
ruby -v: ruby 1.9.3dev (2010-12-28 trunk 30406) [i386-mswin32_90]

C:\work>irb
irb(main):001:0> Regexp.new("\xFF")
RegexpError: c:/usr/lib/ruby/1.9.1/irb.rb:168:in `write': "\xFF" on CP949 (Encoding::InvalidByteSequenceError)
        from c:/usr/lib/ruby/1.9.1/irb.rb:168:in `print'
        from c:/usr/lib/ruby/1.9.1/irb.rb:168:in `block (2 levels) in eval_input'
        from c:/usr/lib/ruby/1.9.1/irb.rb:273:in `signal_status'
        from c:/usr/lib/ruby/1.9.1/irb.rb:156:in `block in eval_input'
        from c:/usr/lib/ruby/1.9.1/irb/ruby-lex.rb:243:in `block (2 levels) in each_top_level_statement'
        from c:/usr/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `loop'
        from c:/usr/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `block in each_top_level_statement'
        from c:/usr/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in `catch'
        from c:/usr/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in `each_top_level_statement'
        from c:/usr/lib/ruby/1.9.1/irb.rb:155:in `eval_input'
        from c:/usr/lib/ruby/1.9.1/irb.rb:70:in `block in start'
        from c:/usr/lib/ruby/1.9.1/irb.rb:69:in `catch'
        from c:/usr/lib/ruby/1.9.1/irb.rb:69:in `start'
        from c:/usr/bin/irb.bat:19:in `<main>'
C:\work>

C:\work>irb
irb(main):001:0> /\xFF/
SyntaxError: c:/usr/lib/ruby/1.9.1/irb.rb:168:in `write': "\xB0" to UTF-8 in conversion from ASCII-8BIT to UTF-8 to UTF-16LE (Encoding::UndefinedConversionError)
        from c:/usr/lib/ruby/1.9.1/irb.rb:168:in `print'
        from c:/usr/lib/ruby/1.9.1/irb.rb:168:in `block (2 levels) in eval_input'
        from c:/usr/lib/ruby/1.9.1/irb.rb:273:in `signal_status'
        from c:/usr/lib/ruby/1.9.1/irb.rb:156:in `block in eval_input'
        from c:/usr/lib/ruby/1.9.1/irb/ruby-lex.rb:243:in `block (2 levels) in each_top_level_statement'
        from c:/usr/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `loop'
        from c:/usr/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `block in each_top_level_statement'
        from c:/usr/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in `catch'
        from c:/usr/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in `each_top_level_statement'
        from c:/usr/lib/ruby/1.9.1/irb.rb:155:in `eval_input'
        from c:/usr/lib/ruby/1.9.1/irb.rb:70:in `block in start'
        from c:/usr/lib/ruby/1.9.1/irb.rb:69:in `catch'
        from c:/usr/lib/ruby/1.9.1/irb.rb:69:in `start'
        from c:/usr/bin/irb.bat:19:in `<main>'
C:\work>

I found that this bug is releated with rb_w32_write_console function.
Here is a possible patch.

--- win32.c     2010-12-28 13:59:30.000000000 +0900
+++ win32.c.new 2010-12-28 14:00:18.000000000 +0900
@@ -5312,7 +5312,7 @@
        !rb_econv_has_convpath_p(rb_enc_name(rb_enc_get(str)), "UTF-16LE"))
        return -1L;

-    str = rb_str_encode(str, rb_enc_from_encoding(rb_enc_find("UTF-16LE")), 0,
+    str = rb_str_encode(str, rb_enc_from_encoding(rb_enc_find("UTF-16LE")), ECONV_INVALID_REPLACE|ECONV_UNDEF_REPLACE,
                        Qnil);
     if (!WriteConsoleW(handle, (LPWSTR)RSTRING_PTR(str), RSTRING_LEN(str)/2,
                       &reslen, NULL)) {


----------------------------------------
http://redmine.ruby-lang.org