From 9237049efe48ce61219aeacf855da90e6e7602fb Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 25 Jan 2018 13:10:14 +0000 Subject: [PATCH] string.c: clear substring code range * string.c (str_substr): substring of broken code range string may be valid or broken. patch by tommy (Masahiro Tomita) at [ruby-dev:50430] [Bug #14388]. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62040 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- string.c | 1 + test/ruby/test_string.rb | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/string.c b/string.c index 82fa603ada..9079387fac 100644 --- a/string.c +++ b/string.c @@ -2560,6 +2560,7 @@ str_substr(VALUE str, long beg, long len, int empty) str2 = str_new_shared(rb_obj_class(str2), str2); RSTRING(str2)->as.heap.ptr += ofs; RSTRING(str2)->as.heap.len = len; + ENC_CODERANGE_CLEAR(str2); } else { if (!len && !empty) return Qnil; diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index b0c6e447eb..dd3a0349b5 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -3087,6 +3087,12 @@ CODE assert_equal("\u3042", "\u3042\u3043".chr) assert_equal('', ''.chr) end + + def test_substr_code_range + data = "\xff" + "a"*200 + assert_not_predicate(data, :valid_encoding?) + assert_predicate(data[100..-1], :valid_encoding?) + end end class TestString2 < TestString