Project

General

Profile

Actions

Bug #6099

closed

[BUG] probable buffer overflow

Added by usa (Usaku NAKAMURA) about 12 years ago. Updated about 12 years ago.

Status:
Closed
Target version:
ruby -v:
ruby 2.0.0dev (2012-02-27 trunk 34828) [i386-netbsdelf]
Backport:
[ruby-dev:45296]

Description

ふと思い立って以下のようなコードを実行してみたところ、表題の[BUG]となりました。
あんまり重大ではないと考えますが、一応報告しておきます。
他のメソッドでも似たようなことができるものはあると思います。
なお、プラットフォーム依存はないはずです。

% ruby -e '
r, w = IO.pipe
buf = " " * 100
Thread.new{p r.sysread(100, buf)}
Thread.pass
buf.replace("")
p buf.bytesize; w.write("a" * 100)
Thread.pass
'

Updated by Glass_saga (Masaki Matsushita) about 12 years ago

=begin
IO#readpartialにも同様の問題があるようです。

require "fcntl"

r, w = IO.pipe
r.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK)
buf = " " * 100
t = Thread.new{p r.readpartial(100, buf)}
sleep 0.1
Thread.pass
buf.replace("")
p buf.bytesize; w.write("a" * 100)
Thread.pass
t.join
=end

Actions #2

Updated by nobu (Nobuyoshi Nakada) about 12 years ago

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

This issue was solved with changeset r34846.
Usaku, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • io.c (io_fread, io_getpartial, rb_io_sysread): set buffer size
    after check if readable, which can cause thread switch.
    [ruby-dev:45297][Bug #6099]

Updated by naruse (Yui NARUSE) about 12 years ago

  • Status changed from Closed to Assigned
  • Assignee set to nobu (Nobuyoshi Nakada)
  • Priority changed from 3 to Normal

Masaki Matsushita wrote:

IO#readpartialにも同様の問題があるようです。

require "fcntl"

r, w = IO.pipe
r.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK)
buf = " " * 100
t = Thread.new{p r.readpartial(100, buf)}
sleep 0.1
Thread.pass
buf.replace("")
p buf.bytesize; w.write("a" * 100)
Thread.pass
t.join

IO#readpartial は

  • It blocks only if ios has no data immediately available.

  • The later means that readpartial is nonblocking-flag insensitive.

  • It blocks on the situation IO#sysread causes Errno::EWOULDBLOCK as if the fd is blocking mode.
    などとある通り、r.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK) しても block することがあります。
    よって、[BUG] が出たら明らかにおかしいんですが、そのコードが通ることを期待するのも間違っています。

Updated by Glass_saga (Masaki Matsushita) about 12 years ago

Yui NARUSE wrote:

IO#readpartial は

  • It blocks only if ios has no data immediately available.
  • The later means that readpartial is nonblocking-flag insensitive.
  • It blocks on the situation IO#sysread causes Errno::EWOULDBLOCK as if the fd is blocking mode.
    などとある通り、r.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK) しても block することがあります。
    よって、[BUG] が出たら明らかにおかしいんですが、そのコードが通ることを期待するのも間違っています。

IO#readpartialも場合によってはblockする事があるんですね。
勉強になりました。

Updated by naruse (Yui NARUSE) about 12 years ago

  • Status changed from Assigned to Closed
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0