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

----------------------------------------
Bug #13993: Psych::Emitter が NUL終端文字列を期待している
https://bugs.ruby-lang.org/issues/13993

* Author: tommy (Masahiro Tomita)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.5.0dev (2017-10-10 trunk 60154) [x86_64-linux]
* Backport: 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
Psych::Emitter が NUL終端文字列を期待していて、SHARABLE_MIDDLE_SUBSTRING=1 時におかしくなります。

次のプログラム(Psychに詳しくないので変なコードかもしれません)を実行すると、

```ruby
require 'psych'
require 'stringio'

output = StringIO.new
emitter = Psych::Emitter.new(output)

s = "a"*100

emitter.start_stream(Psych::Parser::UTF8)
emitter.start_document([1, 1], [], false)
emitter.scalar("x", s[0,30], nil, true, false, Psych::Nodes::Scalar::PLAIN)
emitter.end_document(false)
emitter.end_stream

puts output.string
```

結果が次のようになります。

```
%YAML 1.1
--- &aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa x
...
```

パッチ適用後は次のようになります。

```
%YAML 1.1
--- &aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa x
...
```

パッチ:

```diff
diff --git a/ext/psych/psych_emitter.c b/ext/psych/psych_emitter.c
index bb4c4b226b..55bd417004 100644
--- a/ext/psych/psych_emitter.c
+++ b/ext/psych/psych_emitter.c
@@ -272,8 +272,8 @@ static VALUE scalar(
 
     yaml_scalar_event_initialize(
 	    &event,
-	    (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValuePtr(anchor)),
-	    (yaml_char_t *)(NIL_P(tag) ? NULL : StringValuePtr(tag)),
+	    (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValueCStr(anchor)),
+	    (yaml_char_t *)(NIL_P(tag) ? NULL : StringValueCStr(tag)),
 	    (yaml_char_t*)StringValuePtr(value),
 	    (int)RSTRING_LEN(value),
 	    plain ? 1 : 0,
@@ -319,8 +319,8 @@ static VALUE start_sequence(
 
     yaml_sequence_start_event_initialize(
 	    &event,
-	    (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValuePtr(anchor)),
-	    (yaml_char_t *)(NIL_P(tag) ? NULL : StringValuePtr(tag)),
+	    (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValueCStr(anchor)),
+	    (yaml_char_t *)(NIL_P(tag) ? NULL : StringValueCStr(tag)),
 	    implicit ? 1 : 0,
 	    (yaml_sequence_style_t)NUM2INT(style)
 	    );
@@ -383,8 +383,8 @@ static VALUE start_mapping(
 
     yaml_mapping_start_event_initialize(
 	    &event,
-	    (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValuePtr(anchor)),
-	    (yaml_char_t *)(NIL_P(tag) ? NULL : StringValuePtr(tag)),
+	    (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValueCStr(anchor)),
+	    (yaml_char_t *)(NIL_P(tag) ? NULL : StringValueCStr(tag)),
 	    implicit ? 1 : 0,
 	    (yaml_mapping_style_t)NUM2INT(style)
 	    );
@@ -432,7 +432,7 @@ static VALUE alias(VALUE self, VALUE anchor)
 
     yaml_alias_event_initialize(
 	    &event,
-	    (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValuePtr(anchor))
+	    (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValueCStr(anchor))
 	    );
 
     emit(emitter, &event);
diff --git a/ext/psych/psych_yaml_tree.c b/ext/psych/psych_yaml_tree.c
index bcf24d2070..7aca9114c9 100644
--- a/ext/psych/psych_yaml_tree.c
+++ b/ext/psych/psych_yaml_tree.c
@@ -9,7 +9,7 @@ VALUE cPsychVisitorsYamlTree;
  */
 static VALUE private_iv_get(VALUE self, VALUE target, VALUE prop)
 {
-    return rb_attr_get(target, rb_intern(StringValuePtr(prop)));
+    return rb_attr_get(target, rb_intern(StringValueCStr(prop)));
 }
 
 void Init_psych_yaml_tree(void)
```



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