Project

General

Profile

Bug #13993

Psych::Emitter が NUL終端文字列を期待している

Added by tommy (Masahiro Tomita) over 1 year ago. Updated over 1 year ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.5.0dev (2017-10-10 trunk 60154) [x86_64-linux]
[ruby-dev:50285]

Description

Psych::Emitter が NUL終端文字列を期待していて、SHARABLE_MIDDLE_SUBSTRING=1 時におかしくなります。

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

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 --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)

Associated revisions

Revision 865decb5
Added by hsbt (Hiroshi SHIBATA) over 1 year ago

null byte at Psych::Emitter.

Check null byte. Patched by tommy (Masahiro Tomita).
[Bug #13993][ruby-dev:50285]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60588 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 60588
Added by hsbt (Hiroshi SHIBATA) over 1 year ago

null byte at Psych::Emitter.

Check null byte. Patched by tommy (Masahiro Tomita).
[Bug #13993][ruby-dev:50285]

Revision 60588
Added by hsbt (Hiroshi SHIBATA) over 1 year ago

null byte at Psych::Emitter.

Check null byte. Patched by tommy (Masahiro Tomita).
[Bug #13993][ruby-dev:50285]

Revision 60588
Added by hsbt (Hiroshi SHIBATA) over 1 year ago

null byte at Psych::Emitter.

Check null byte. Patched by tommy (Masahiro Tomita).
[Bug #13993][ruby-dev:50285]

History

#1

Updated by hsbt (Hiroshi SHIBATA) over 1 year ago

  • Status changed from Open to Closed

Applied in changeset trunk|r60588.


null byte at Psych::Emitter.

Check null byte. Patched by tommy (Masahiro Tomita).
[Bug #13993][ruby-dev:50285]

Also available in: Atom PDF