1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

string.c: fill the terminator

* string.c (str_replace_shared_without_enc): fill the terminator
  of embedded strings in wide char encodings.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51372 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2015-07-25 02:05:34 +00:00
parent 9e4b24d4f3
commit 8101cc3de4
3 changed files with 26 additions and 5 deletions

View file

@ -1,3 +1,8 @@
Sat Jul 25 11:05:31 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
* string.c (str_replace_shared_without_enc): fill the terminator
of embedded strings in wide char encodings.
Sat Jul 25 06:38:36 2015 Koichi Sasada <ko1@atdot.net> Sat Jul 25 06:38:36 2015 Koichi Sasada <ko1@atdot.net>
* vm_core.h: size should be unsigned. * vm_core.h: size should be unsigned.

View file

@ -962,16 +962,24 @@ rb_str_export_to_enc(VALUE str, rb_encoding *enc)
static VALUE static VALUE
str_replace_shared_without_enc(VALUE str2, VALUE str) str_replace_shared_without_enc(VALUE str2, VALUE str)
{ {
if (RSTRING_LEN(str) <= RSTRING_EMBED_LEN_MAX) { const int termlen = TERM_LEN(str);
char *ptr;
long len;
RSTRING_GETMEM(str, ptr, len);
if (len+termlen <= RSTRING_EMBED_LEN_MAX+1) {
char *ptr2 = RSTRING(str2)->as.ary;
STR_SET_EMBED(str2); STR_SET_EMBED(str2);
memcpy(RSTRING_PTR(str2), RSTRING_PTR(str), RSTRING_LEN(str)+1); memcpy(ptr2, RSTRING_PTR(str), len);
STR_SET_EMBED_LEN(str2, RSTRING_LEN(str)); STR_SET_EMBED_LEN(str2, len);
TERM_FILL(ptr2+len, termlen);
} }
else { else {
str = rb_str_new_frozen(str); str = rb_str_new_frozen(str);
FL_SET(str2, STR_NOEMBED); FL_SET(str2, STR_NOEMBED);
RSTRING(str2)->as.heap.len = RSTRING_LEN(str); RSTRING_GETMEM(str, ptr, len);
RSTRING(str2)->as.heap.ptr = RSTRING_PTR(str); RSTRING(str2)->as.heap.len = len;
RSTRING(str2)->as.heap.ptr = ptr;
STR_SET_SHARED(str2, str); STR_SET_SHARED(str2, str);
} }
return str2; return str2;

View file

@ -86,6 +86,14 @@ class Test_StringCStr < Test::Unit::TestCase
} }
end end
def test_wchar_replace
assert_wchars_term_char("abc") {|s|
w = s.dup
s.replace("abcdefghijklmnop")
s.replace(w)
}
end
def test_embedded_from_heap def test_embedded_from_heap
gh821 = "[GH-821]" gh821 = "[GH-821]"
embedded_string = "abcdefghi" embedded_string = "abcdefghi"