summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2012-06-10 22:16:05 -0700
committerYukihiro "Matz" Matsumoto <[email protected]>2012-06-10 22:16:05 -0700
commit5ca2d5e0ee6127d33e2b0d18a12e975f5235cae4 (patch)
treea9967ec1be0bb0636ab11630b2eef20dbc4556d8
parentbd83a460c3c1f4ac8987657ab63bad87b59caf02 (diff)
parent3bb09f99284d6da3ee5192fb4636ad73981ad1f1 (diff)
downloadmruby-5ca2d5e0ee6127d33e2b0d18a12e975f5235cae4.tar.gz
mruby-5ca2d5e0ee6127d33e2b0d18a12e975f5235cae4.zip
Merge pull request #256 from masamitsu-murase/modify_string_slice
"slice" of shared string returns invalid result
-rw-r--r--src/string.c10
-rw-r--r--test/t/string.rb5
2 files changed, 9 insertions, 6 deletions
diff --git a/src/string.c b/src/string.c
index e955b3f22..67cc32137 100644
--- a/src/string.c
+++ b/src/string.c
@@ -1199,14 +1199,14 @@ mrb_str_eql(mrb_state *mrb, mrb_value self)
static mrb_value
mrb_str_subseq(mrb_state *mrb, mrb_value str, int beg, int len)
{
- struct RString *s;
+ struct RString *orig, *s;
struct mrb_shared_string *shared;
- s = mrb_str_ptr(str);
- str_make_shared(mrb, s);
- shared = s->aux.shared;
+ orig = mrb_str_ptr(str);
+ str_make_shared(mrb, orig);
+ shared = orig->aux.shared;
s = mrb_obj_alloc_string(mrb);
- s->ptr = shared->ptr + beg;
+ s->ptr = orig->ptr + beg;
s->len = len;
s->aux.shared = shared;
s->flags |= MRB_STR_SHARED;
diff --git a/test/t/string.rb b/test/t/string.rb
index 964ec0e63..d7182fc38 100644
--- a/test/t/string.rb
+++ b/test/t/string.rb
@@ -256,6 +256,9 @@ assert('String#slice', '15.2.10.5.34') do
d1 = 'abc'.slice(0, 0)
e1 = 'abc'.slice(1, 2)
+ # slice of shared string
+ e11 = e1.slice(0)
+
# args is RegExp
# TODO SEGFAULT ATM
@@ -265,7 +268,7 @@ assert('String#slice', '15.2.10.5.34') do
a == 'a' and b == 'c' and c == nil and d == nil and
a1 == nil and b1 == nil and c1 == nil and d1 == '' and
- e1 == 'bc' and
+ e1 == 'bc' and e11 == 'b' and
a3 == 'bc' and b3 == nil
end