Project

General

Profile

Bug #17117

Updated by peterzhu2118 (Peter Zhu) over 4 years ago

Extension string stored in `ARGF.inplace` is created using an api designed for C string constants to create a Ruby string that points at another Ruby string. When the original string is swept, the extension string gets corrupted. 

 Reproduction script (on MacOS): 

 ```ruby 
 #!/usr/bin/ruby -pi.bak 

 BEGIN { 
   GC.start(full_mark: true) 
   arr = [] 
   1000000.times do |x| 
     arr << "fooo#{x}" 
   end 
 } 

 puts "hello" 
 ``` 

 [Fix PR on GitHub](https://github.com/ruby/ruby/pull/3409) 

 # Reproduction 

 1. Assuming you have the above script in a file called `test.rb`. 
 2. Create a file called `foo.txt` with contents `foo`. 
 3. Run `ruby test.rb foo.txt`. 

 # Expected behaviour 

 `foo.txt` with contents `hello\nfoo` and `foo.txt.bak` with contents `foo`. 

 # Actual behavior 

 `foo.txt` with contents `hello\nfoo` and `foo.txto121` with contents `foo`. 

Back