From: "caplanmaor (Maor Caplan) via ruby-core" Date: 2026-11-23T20:03:21+00:00 Subject: [ruby-core:127687] [Ruby Bug#22631] Set#map! / Set#collect! segfaults under GC.stress on 4.0.x Issue #22631 has been reported by caplanmaor (Maor Caplan). ---------------------------------------- Bug #22631: Set#map! / Set#collect! segfaults under GC.stress on 4.0.x https://bugs.ruby-lang.org/issues/22631 * Author: caplanmaor (Maor Caplan) * Status: Open * ruby -v: ruby 4.0.10 (2026-11-19 revision a704b7cd95) +PRISM [x86_64-linux] * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- `Set#collect!`/`map!` on an empty set segfaults when a GC runs during the call. ## Reproduction ```ruby GC.stress = true 1000.times do Set.new.map! { |x| x } end ``` Segfaults on the first iteration, every run, on the official `ruby:4.0.10` Docker image (gcc (Debian 14.2.0-19) 14.2.0). Also reproduced on 4.0.9 and 4.0.7. ``` repro.rb:4: [BUG] Segmentation fault at 0x0000000000000000 ruby 4.0.10 (2026-11-19 revision a704b7cd95) +PRISM [x86_64-linux] -- C level backtrace information ------------------------------------------- libruby.so.4.0(memcpy+0xf) /usr/include/x86_64-linux-gnu/bits/string_fortified.h:29 libruby.so.4.0(ruby_nonempty_memcpy) ./include/ruby/internal/memory.h:759 libruby.so.4.0(rb_set_copy) ./include/ruby/internal/memory.h:756 libruby.so.4.0(set_i_initialize_copy+0x5) /usr/src/ruby/set.c:566 libruby.so.4.0(set_i_collect) /usr/src/ruby/set.c:1473 ``` The `memcpy` source pointer (`RSI`) is NULL and the size (`RDX`) is 0x40. ## Cause `set_i_collect` builds a temporary set and copies it back into the receiver with `set_i_initialize_copy`. Nothing keeps the temporary alive across the `set_copy` call, so a GC triggered by the allocation inside `rb_set_copy` frees it and the `memcpy` then reads from the freed table. ---Files-------------------------------- ruby-set-map-crash.txt (23.1 KB) -- 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/