Bug #2781

crash when gc_mark()ing already free'd locals of cloned scope

Added by coderrr (coderrr .) about 9 years ago. Updated about 8 years ago.

Target version:
ruby -v:
>= 1.8.7-p248


This causes a segfault on >= 1.8.7-p248

def def_x(arg)
Object.send :define_method, :x do
def_x lambda{}

GC.stress = true # unnecessary but makes it occur faster
def_x nil
n = 3 # minimum for crash, increase if needed
n.times { x 0 }

This bug was caused by the fix i suggested for #1322,

The previous fix is flawed in that it added the SCOPE_MALLOC flag to the scope just so scope_dup() didn't process it. This had the side-effect that gc_mark_children() now processes the scope whereas it would not have before. A better fix is the following, which instead of adding the SCOPE_MALLOC flag, we add a check for the SCOPE_CLONE flag to scope_dup(). This fixes bug #1322 as well as the segfault:

Please check the patch for other unforseen side effects. I didn't see any changes in rubyspec failures from p174 to a patched p248.



Updated by coderrr (coderrr .) about 9 years ago

just realized the check for SCOPE_CLONE is also no longer needed before freeing locals:


Updated by coderrr (coderrr .) about 9 years ago

By the way, this causes the popular web framework sinatra to segfault due to


Updated by tmm1 (Aman Gupta) over 8 years ago

I can confirm that this is still an issue in 1.8.7-p302 (I had to increase n=3000 to reproduce on linux).

It is also causing segfaults when using Sinatra <= 0.9.5. The segfaults in Sinatra are fixed as of >= 0.9.6 with this patch:

Also available in: Atom PDF