Issue #5714 has been updated by Heesob Park.


I guess this issue is due to a bug of _read function of Microsoft Runtime library.

Here is a patch for workaround.

diff --git a/win32.c b/win32.c.new
index 67a392e..4d5a253 100644
--- a/win32.c
+++ b/win32.c.new
@@ -5451,7 +5451,8 @@ rb_w32_read(int fd, void *buf, size_t size)
 	return -1;
     }
 
-    if (_osfile(fd) & FTEXT) {
+    isconsole = is_console(_osfhnd(fd));
+    if (!isconsole && (_osfile(fd) & FTEXT)) {
 	return _read(fd, buf, size);
     }
 
@@ -5464,7 +5465,6 @@ rb_w32_read(int fd, void *buf, size_t size)
     }
 
     ret = 0;
-    isconsole = is_console(_osfhnd(fd));
     if (isconsole) {
 	DWORD mode;
 	GetConsoleMode((HANDLE)_osfhnd(fd),&mode);

----------------------------------------
Bug #5714: Unexpected error of STDIN#read with non-ascii input on Windows XP
http://redmine.ruby-lang.org/issues/5714

Author: Heesob Park
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 
ruby -v: ruby 2.0.0dev (2011-12-05 trunk 33955) [i386-mswin32_100]


When the input contains non-ascii character, STDIN#read raised Permission denied or Invalid argument error with MSVC compiled version on Windows XP.

	C:\work>ruby -ve 'p STDIN.read(5)'
	ruby 2.0.0dev (2011-12-05 trunk 33955) [i386-mswin32_100]
	???κΈ?abcd
	-e:1:in `read': Permission denied - <STDIN> (Errno::EACCES)
		from -e:1:in `<main>'

	C:\>irb
	irb(main):001:0> STDIN.read(5)
	???κΈ?abcd
	Errno::EINVAL: Invalid argument - <STDIN>
		from (irb):1:in `read'
		from (irb):1
		from c:/usr/bin/irb.bat:19:in `<main>'

If the input is ascii only, STDIN.read works fine.

	C:\work>ruby -ve 'p STDIN.read(5)'
	ruby 2.0.0dev (2011-12-05 trunk 33955) [i386-mswin32_100]
	abcdefg
	"abcde"

	C:\>irb
	irb(main):001:0> STDIN.read(5)
	abcdefg
	=> "abcde"

It is odd but the Mingw compiled version works fine.

	C:\work>ruby -ve 'p STDIN.read(5)'
	ruby 2.0.0dev (2011-12-05 trunk 33955) [i386-mingw32]
	???κΈ?abcde
	"\xC7\xD1\xB1\xDBabc"

And Ruby 1.9.3p0 works fine.
	
	C:\>ruby -ve 'p STDIN.read(5)'
	ruby 1.9.3p0 (2011-10-30 revision 33570) [i386-mswin32_100]
	???κΈ?abcd
	"\xC7\xD1\xB1\xDBabc"



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