Project

General

Profile

Bug #3014

select + read for STDIN unexpected result on Windows

Added by phasis68 (Heesob Park) over 10 years ago. Updated over 9 years ago.

Status:
Closed
Priority:
Normal
Target version:
ruby -v:
ruby 1.9.2dev (2010-03-26 trunk 27048) [i386-mswin32_90]
Backport:
[ruby-core:29018]

Description

=begin
I found that select and read combination for STDIN results "r u b y . e x e" on Windows.

Consider following code:
select([STDIN],nil,nil,10);puts STDIN.read(8)

I entered "test1234\n" for input.

On 1.8.6, it works fine.

C:>ruby -ve 'select([STDIN],nil,nil,10);puts STDIN.read(8)'
ruby 1.8.6 (2010-02-04 patchlevel 398) [i386-mingw32]
test1234
test1234

On 1.9.x, it results "r u b y".

C:\work>ruby -ve 'select([STDIN],nil,nil,10);puts STDIN.read(8)'
ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-mingw32]
test1234
r u b y

C:\work>ruby -ve 'select([STDIN],nil,nil,10);puts STDIN.read(8)'
ruby 1.9.2dev (2010-03-26 trunk 27048) [i386-mswin32_90]
test1234
r u b y

C:\work>ruby -ve 'select([STDIN],nil,nil,10);p STDIN.read(8)'
ruby 1.9.2dev (2010-03-26 trunk 27048) [i386-mswin32_90]
test1234
"r\x00u\x00b\x00y\x00"

The result string is actually "r u b y . e x e" plus some garbage.
=end

#1

Updated by yugui (Yuki Sonoda) over 10 years ago

  • Assignee set to usa (Usaku NAKAMURA)
  • Target version changed from 2.0.0 to 1.9.2

=begin

=end

#2

Updated by phasis68 (Heesob Park) over 10 years ago

=begin

I found that STDIN.read(n) works when n >= 41.

I guess this bug is caused by PeekConsoleInput's bug or side effect.

Here is a guick and dirty patch.

--- win32.c 2010-03-26 19:04:28.502025030 +0900
+++ win32.c.new 2010-03-26 19:03:05.138310126 +0900
@@ -4851,6 +4851,10 @@
return _read(fd, buf, size);
}

  • if((_osfile(fd) & FDEV) && size <= 40) {
  • return fread(buf, 1, size, stdin);
  • }
    +
    MTHREAD_ONLY(EnterCriticalSection(&(_pioinfo(fd)->lock)));

    if (!size || _osfile(fd) & FEOFLAG) {

=end

#3

Updated by usa (Usaku NAKAMURA) over 10 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

=begin
This issue was solved with changeset r28035.
Heesob, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.

=end

Also available in: Atom PDF