From: "nobu (Nobuyoshi Nakada) via ruby-core" Date: 2026-12-05T17:15:06+00:00 Subject: [ruby-core:127891] [Ruby Bug#22716] ARGF uses a non-IO $stdin as an IO when ARGV contains "-" Issue #22716 has been updated by nobu (Nobuyoshi Nakada). Status changed from Open to Closed Closed by commit:2d2b34a7b83051a57e7dfa61439e29db8d112716 ---------------------------------------- Bug #22716: ARGF uses a non-IO $stdin as an IO when ARGV contains "-" https://bugs.ruby-lang.org/issues/22716#change-120503 * Author: saddestmartian (sad mart) * Status: Closed * ruby -v: ruby 4.1.0dev (2026-12-03T23:00:19Z master bcd243e8e8) +PRISM [x86_64-linux] * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- `$stdin` can be any object, and ARGF reads through it when it is not an IO. When `ARGV` contains `"-"`, however, `argf_next_argv()` sets the current file to `$stdin` and then uses it as an IO (`rb_io_ascii8bit_binmode()`, `GetOpenFile()`, writes to `fptr->encs`) without checking its type: ``` $ ruby -rstringio -e '$stdin = StringIO.new("x\n"); p readlines' - ``` Expected: `["x\n"]`, as without `-`. Actual on master and 4.0.10: `-e:1:in 'ARGF.class#readlines': uninitialized stream (IOError)`. Actual on 3.4.13: `closed stream (IOError)`, or a segmentation fault in some runs. Other `$stdin` objects, such as objects with instance variables or delegators like `Tempfile`, crash instead of raising, because their memory is read and written as an `rb_io_t`. For example, on master and 4.0.10: ```ruby # ruby poc.rb - buf = "\0".b * 200 o = Object.new o.instance_variable_set(:@buf, buf) def o.binmode = self def o.gets(*) = nil $stdin = o ARGF.binmode p buf.bytesize #=> 17179869384, expected 200 ``` `ARGF.set_encoding` has the same problem after forwarding to a non-IO `$stdin`, with or without `-`. Reproduced on 3.2.11, 3.3.14, 3.4.13, 4.0.10 and master. A patch with tests: https://github.com/ruby/ruby/pull/20477 -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/