Issue #13960 has been reported by tommy (Masahiro Tomita).

----------------------------------------
Bug #13960: ARGF.inplace_mode= が SHARABLE_MIDDLE_SUBSTRING=1 を考慮していない
https://bugs.ruby-lang.org/issues/13960

* Author: tommy (Masahiro Tomita)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.5.0dev (2017-10-01 trunk 60085) [x86_64-linux]
* Backport: 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
SHARABLE_MIDDLE_SUBSTRING=1 でコンパイルしたRubyで、ARGF.inplace_mode= がNUL終端まで文字列を使用してしまいます。

```
% echo hoge > hoge
% ruby -e 'ARGF.inplace_mode=(".abcdefghijklmnopqrstuvwxyz"*2)[0,27]; ARGF.each_line{|line| print line.sub("hoge","fuga")}' hoge
% ls hoge*   
hoge  hoge.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz
```

次のパッチで直ると思います。

```diff
diff --git a/io.c b/io.c
index 1254e92363..ed4a562d8b 100644
--- a/io.c
+++ b/io.c
@@ -12224,7 +12224,7 @@ argf_inplace_mode_set(VALUE argf, VALUE val)
 	ARGF.inplace = 0;
     }
     else {
-	StringValue(val);
+	StringValueCStr(val);
 	if (ARGF.inplace) free(ARGF.inplace);
 	ARGF.inplace = 0;
 	ARGF.inplace = strdup(RSTRING_PTR(val));
```



-- 
https://bugs.ruby-lang.org/