diff options
author | shyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2019-01-15 06:41:58 +0000 |
---|---|---|
committer | shyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2019-01-15 06:41:58 +0000 |
commit | d154bec0d5f0dd32e0d30559ab8a0a7ed8be98d2 (patch) | |
tree | 742aefd4559c4b84b11de474ae6fb005c037faee /string.c | |
parent | 5e84537d32c3e2f21071384e2c967fe0308d408d (diff) |
setbyte / ungetbyte allow out-of-range integers
* string.c: String#setbyte to accept arbitrary integers [Bug #15460]
* io.c: ditto for IO#ungetbyte
* ext/strringio/stringio.c: ditto for StringIO#ungetbyte
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66824 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r-- | string.c | 9 |
1 files changed, 4 insertions, 5 deletions
@@ -5409,7 +5409,6 @@ static VALUE rb_str_setbyte(VALUE str, VALUE index, VALUE value) { long pos = NUM2LONG(index); - int byte = NUM2INT(value); long len = RSTRING_LEN(str); char *head, *left = 0; unsigned char *ptr; @@ -5420,10 +5419,10 @@ rb_str_setbyte(VALUE str, VALUE index, VALUE value) rb_raise(rb_eIndexError, "index %ld out of string", pos); if (pos < 0) pos += len; - if (byte < 0) - rb_raise(rb_eRangeError, "integer %d too small to convert into `unsigned char'", byte); - if (UCHAR_MAX < byte) - rb_raise(rb_eRangeError, "integer %d too big to convert into `unsigned char'", byte); + + VALUE v = rb_to_int(value); + VALUE w = rb_int_modulo(v, INT2FIX(256)); + unsigned char byte = NUM2INT(w) & 0xFF; if (!str_independent(str)) str_make_independent(str); |