From 4cb618e7aacf84cf7c241ee0513bbb37e1068c9d Mon Sep 17 00:00:00 2001 From: naruse Date: Thu, 17 Jan 2019 21:36:17 +0000 Subject: merge revision(s) 66760,66761,66824: [Backport #15460] Follow behaviour of IO#ungetbyte see r65802 and [Bug #14359] * expand tabs. 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/branches/ruby_2_6@66845 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- string.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'string.c') diff --git a/string.c b/string.c index 71afc98ba5..dcb28532cd 100644 --- a/string.c +++ b/string.c @@ -5411,7 +5411,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; @@ -5422,10 +5421,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); -- cgit v1.2.3