From 483405255628760da9af73fc07d47e1f920f9c0c Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 23 Mar 2017 22:33:41 +0900 Subject: [PATCH] bugfix: string-copy issue --- lib/string.c | 10 ++++------ t/issue/string-copy.scm | 8 ++++++++ 2 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 t/issue/string-copy.scm diff --git a/lib/string.c b/lib/string.c index 832d4e3b..9a2ebd50 100644 --- a/lib/string.c +++ b/lib/string.c @@ -80,22 +80,20 @@ make_rope_lit(pic_state *pic, const char *str, int len) static struct rope * make_rope_slice(pic_state *pic, struct rope *owner, int i, int j) { - struct rope *rope; + struct rope *rope, *real_owner; assert(owner->isleaf); - if (owner->u.leaf.owner != NULL) { - owner = owner->u.leaf.owner; - } + real_owner = owner->u.leaf.owner == NULL ? owner : owner->u.leaf.owner; rope = pic_malloc(pic, offsetof(struct rope, buf)); rope->refcnt = 1; rope->weight = j - i; rope->isleaf = true; - rope->u.leaf.owner = owner; + rope->u.leaf.owner = real_owner; rope->u.leaf.str = owner->u.leaf.str + i; - pic_rope_incref(owner); + pic_rope_incref(real_owner); return rope; } diff --git a/t/issue/string-copy.scm b/t/issue/string-copy.scm new file mode 100644 index 00000000..2e22c0ee --- /dev/null +++ b/t/issue/string-copy.scm @@ -0,0 +1,8 @@ +(import (scheme base) + (picrin test)) + +(test-begin) + +(test "456" (string-copy (string-copy "1234567" 3) 0 3)) + +(test-end)