8000 Follow behaviour of IO#ungetbyte · github/ruby@730f288 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 730f288

Browse files
committed
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
1 parent bc27f87 commit 730f288

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

ext/stringio/stringio.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -803,16 +803,27 @@ static VALUE
803803
strio_ungetbyte(VALUE self, VALUE c)
804804
{
805805
struct StringIO *ptr = readable(self);
806-
char buf[1], *cp = buf;
807-
long cl = 1;
808806

809807
check_modifiable(ptr);
810808
if (NIL_P(c)) return Qnil;
811809
if (FIXNUM_P(c)) {
812-
buf[0] = (char)FIX2INT(c);
813-
return strio_unget_bytes(ptr, buf, 1);
810+
int i = FIX2INT(c);
811+
if (0 <= i && i <= UCHAR_MAX) {
812+
char buf[1];
813+
buf[0] = (char)i;
814+
return strio_unget_bytes(ptr, buf, 1);
815+
}
816+
else {
817+
rb_raise(rb_eRangeError,
818+
"integer %d too big to convert into `unsigned char'", i);
819+
}
820+
}
821+
else if (RB_TYPE_P(c, T_BIGNUM)) {
822+
rb_raise(rb_eRangeError, "bignum too big to convert into `unsigned char'");
814823
}
815824
else {
825+
char *cp;
826+
long cl;
816827
SafeStringValue(c);
817828
cp = RSTRING_PTR(c);
818829
cl = RSTRING_LEN(c);

test/stringio/test_stringio.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,10 @@ def test_ungetbyte
452452
t.ungetbyte("\u{30eb 30d3 30fc}")
453453
assert_equal(0, t.pos)
454454
assert_equal("\u{30eb 30d3 30fc}\u7d05\u7389bar\n", s)
455+
456+
assert_raise(RangeError) {t.ungetbyte(-1)}
457+
assert_raise(RangeError) {t.ungetbyte(256)}
458+
assert_raise(RangeError) {t.ungetbyte(1<<64)}
455459
end
456460

457461
def test_ungetc

0 commit comments

Comments
 (0)
0