Project

General

Profile

Actions

Bug #22633

closed

Enumerator#peek_values segfaults when the enumerated code calls Fiber.yield with a non-Array

Bug #22633: Enumerator#peek_values segfaults when the enumerated code calls Fiber.yield with a non-Array

Added by kjeldahl (Jacob Kjeldahl) 4 days ago. Updated 4 days ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 4.1.0dev (2026-11-24T08:50:11Z HEAD dec1bb786f) +PRISM [aarch64-linux]
[ruby-core:<unknown>]

Description

Enumerator#peek_values crashes the VM when the enumerated code calls Fiber.yield with a non-Array value. enumerator_peek_values_m calls rb_ary_dup on the lookahead and assumes an Array, but get_next_values returns whatever the fiber yielded.

e = Enumerator.new { |y| Fiber.yield 7; y << 1 }
e.peek_values
-e:1: [BUG] Segmentation fault at 0x000000000000000f

Reproduced with:

ruby 4.1.0dev (2026-11-24T08:50:11Z HEAD dec1bb786f) +PRISM [aarch64-linux]
ruby 4.0.10 (2026-11-19 revision a704b7cd95) +PRISM [aarch64-linux]

Expected: no crash. peek_values is documented to return the next object as an array, so a value yielded by a foreign Fiber.yield should be rejected with an error or wrapped, not treated as an Array.

Details (line numbers are master; 4.0.10 is 18 lines earlier with the same code): get_next_values returns the fiber's resume value (enumerator.c:817), enumerator_peek_values stores it as e->lookahead (945), and enumerator_peek_values_m calls rb_ary_dup on it (984). The fault address is the VALUE of the Fixnum 7. Fiber.yield 1, nil, "s" and {} also segfault on both versions; Fiber.yield Object.new makes peek_values answer []. next_values and next return the raw value instead of an Array.

Possible fix: check T_ARRAY in enumerator_peek_values_m, or tell next_ii's yields apart from a foreign Fiber.yield in get_next_values.

First reported on HackerOne; ruby staff asked me to file it here as a non-security bug.

Updated by mame (Yusuke Endoh) 4 days ago Actions #1

  • Status changed from Open to Closed

Applied in changeset git|8888bcfc418b31483ddc6008681bc8eae6b12c60.


Return a non-Array value from Enumerator#peek_values as is

A Fiber.yield inside the enumerator passes its value as the next values,
which Enumerator#next_values returns as is, but #peek_values tried to
duplicate it as an Array.

[Bug #22633]

Co-Authored-By: Claude Opus 5.5

Actions

Also available in: PDF Atom