Actions
Bug #22631
closedSet#map! / Set#collect! segfaults under GC.stress on 4.0.x
Bug #22631:
Set#map! / Set#collect! segfaults under GC.stress on 4.0.x
Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 4.0.10 (2026-11-19 revision a704b7cd95) +PRISM [x86_64-linux]
Description
Set#collect!/map! on an empty set segfaults when a GC runs during the call.
Reproduction¶
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
Actions