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 /ext | |
parent | 5e84537d32c3e2f21071384e2c967fe0308d408d (diff) | |
download | ruby-d154bec0d5f0dd32e0d30559ab8a0a7ed8be98d2.tar.gz |
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 'ext')
-rw-r--r-- | ext/stringio/stringio.c | 45 |
1 files changed, 19 insertions, 26 deletions
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index 161ce7fd83..47c2c50b95 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -805,33 +805,26 @@ strio_ungetbyte(VALUE self, VALUE c) struct StringIO *ptr = readable(self); check_modifiable(ptr); - if (NIL_P(c)) return Qnil; - if (FIXNUM_P(c)) { - int i = FIX2INT(c); - if (0 <= i && i <= UCHAR_MAX) { - char buf[1]; - buf[0] = (char)i; - return strio_unget_bytes(ptr, buf, 1); - } - else { - rb_raise(rb_eRangeError, - "integer %d too big to convert into `unsigned char'", i); - } - } - else if (RB_TYPE_P(c, T_BIGNUM)) { - rb_raise(rb_eRangeError, "bignum too big to convert into `unsigned char'"); - } - else { - char *cp; - long cl; - SafeStringValue(c); - cp = RSTRING_PTR(c); - cl = RSTRING_LEN(c); - if (cl == 0) return Qnil; - strio_unget_bytes(ptr, cp, cl); - RB_GC_GUARD(c); - return Qnil; + switch (TYPE(c)) { + case T_NIL: + return Qnil; + case T_FIXNUM: + case T_BIGNUM: ; + /* rb_int_modulo() not visible from exts */ + VALUE v = rb_funcall(c, rb_intern("modulo"), 1, INT2FIX(256)); + unsigned char cc = NUM2INT(v) & 0xFF; + c = rb_str_new((const char *)&cc, 1); + break; + default: + SafeStringValue(c); } + + const char *cp = RSTRING_PTR(c); + long cl = RSTRING_LEN(c); + if (cl == 0) return Qnil; + strio_unget_bytes(ptr, cp, cl); + RB_GC_GUARD(c); + return Qnil; } static VALUE |