aboutsummaryrefslogtreecommitdiffstats
path: root/string.c
diff options
context:
space:
mode:
Diffstat (limited to 'string.c')
-rw-r--r--string.c9
1 files changed, 5 insertions, 4 deletions
diff --git a/string.c b/string.c
index f770118660..29dfdb94af 100644
--- a/string.c
+++ b/string.c
@@ -4567,13 +4567,14 @@ rb_str_setbyte(VALUE str, VALUE index, VALUE value)
{
long pos = NUM2LONG(index);
int byte = NUM2INT(value);
+ long len = RSTRING_LEN(str);
- rb_str_modify(str);
-
- if (pos < -RSTRING_LEN(str) || RSTRING_LEN(str) <= pos)
+ if (pos < -len || len <= pos)
rb_raise(rb_eIndexError, "index %ld out of string", pos);
if (pos < 0)
- pos += RSTRING_LEN(str);
+ pos += len;
+
+ rb_str_modify(str);
RSTRING_PTR(str)[pos] = byte;