diff options
author | kazu <kazu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2019-01-09 12:55:20 +0000 |
---|---|---|
committer | kazu <kazu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2019-01-09 12:55:20 +0000 |
commit | 730f2886b535709832494ebe8c98f88ba0a29cea (patch) | |
tree | d751bf63f4df33521833972e227a422f1d28bf00 /ext/stringio | |
parent | bc27f87889934099f1b8b17512c634b615062e22 (diff) |
Follow behaviour of IO#ungetbyte
see r65802 and [Bug #14359]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66760 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/stringio')
-rw-r--r-- | ext/stringio/stringio.c | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index eb23109087..3cb46d99b9 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -803,16 +803,27 @@ static VALUE strio_ungetbyte(VALUE self, VALUE c) { struct StringIO *ptr = readable(self); - char buf[1], *cp = buf; - long cl = 1; check_modifiable(ptr); if (NIL_P(c)) return Qnil; if (FIXNUM_P(c)) { - buf[0] = (char)FIX2INT(c); - return strio_unget_bytes(ptr, buf, 1); + 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); |