diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-04-16 07:12:06 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-04-16 07:12:06 +0000 |
commit | cea438b0ca99bae8ebf925662878850328fa5dea (patch) | |
tree | 347e43bc3698035793335c0297a158d2ecf1307a | |
parent | bfbf7a350a26f40871f938c497ac961bcda94209 (diff) | |
download | ruby-cea438b0ca99bae8ebf925662878850328fa5dea.tar.gz |
string.c: fix dumped suffix
* string.c (rb_str_dump): get rid of an error on evaling with
frozen-string-literal enabled. [ruby-core:86539] [Bug #14687]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63164 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | spec/ruby/core/string/dump_spec.rb | 4 | ||||
-rw-r--r-- | string.c | 12 | ||||
-rw-r--r-- | test/ruby/test_m17n.rb | 5 |
3 files changed, 15 insertions, 6 deletions
diff --git a/spec/ruby/core/string/dump_spec.rb b/spec/ruby/core/string/dump_spec.rb index 503848221c..cfe7bea6f0 100644 --- a/spec/ruby/core/string/dump_spec.rb +++ b/spec/ruby/core/string/dump_spec.rb @@ -418,7 +418,7 @@ describe "String#dump" do end it "includes .force_encoding(name) if the encoding isn't ASCII compatible" do - "\u{876}".encode('utf-16be').dump.should == "\"\\bv\".force_encoding(\"UTF-16BE\")" - "\u{876}".encode('utf-16le').dump.should == "\"v\\b\".force_encoding(\"UTF-16LE\")" + "\u{876}".encode('utf-16be').dump.end_with?(".force_encoding(\"UTF-16BE\")").should be_true + "\u{876}".encode('utf-16le').dump.end_with?(".force_encoding(\"UTF-16LE\")").should be_true end end @@ -5966,7 +5966,7 @@ rb_str_dump(VALUE str) char *q, *qend; VALUE result; int u8 = (encidx == rb_utf8_encindex()); - static const char nonascii_suffix[] = ".force_encoding(\"%s\")"; + static const char nonascii_suffix[] = ".dup.force_encoding(\"%s\")"; len = 2; /* "" */ if (!rb_enc_asciicompat(enc)) { @@ -6285,13 +6285,19 @@ str_undump(VALUE str) break; } else { + static const char force_encoding_suffix[] = ".force_encoding\(\""; + static const char dup_suffix[] = ".dup"; const char *encname; int encidx; ptrdiff_t size; - size = rb_strlen_lit(".force_encoding(\""); + /* check separately for strings dumped by older versions */ + size = sizeof(dup_suffix) - 1; + if (s_end - s > size && memcmp(s, dup_suffix, size) == 0) s += size; + + size = sizeof(force_encoding_suffix) - 1; if (s_end - s <= size) goto invalid_format; - if (memcmp(s, ".force_encoding(\"", size) != 0) goto invalid_format; + if (memcmp(s, force_encoding_suffix, size) != 0) goto invalid_format; s += size; if (utf8) { diff --git a/test/ruby/test_m17n.rb b/test/ruby/test_m17n.rb index e2c0b7633a..80750baf30 100644 --- a/test/ruby/test_m17n.rb +++ b/test/ruby/test_m17n.rb @@ -358,7 +358,10 @@ class TestM17N < Test::Unit::TestCase "\u3042".encode("UTF-16LE"), "\u3042".encode("UTF-16BE"), ].each do |str| - assert_equal(str, eval(str.dump), "[ruby-dev:33142]") + dump = str.dump + assert_equal(str, eval(dump), "[ruby-dev:33142]") + assert_equal(str, dump.undump) + assert_equal(str, eval("# frozen-string-literal: true\n#{dump}"), '[Bug #14687]') end end |