File tree Expand file tree Collapse file tree 2 files changed +19
-4
lines changed Expand file tree Collapse file tree 2 files changed +19
-4
lines changed Original file line number Diff line number Diff line change @@ -803,16 +803,27 @@ static VALUE
803
803
strio_ungetbyte (VALUE self , VALUE c )
804
804
{
805
805
struct StringIO * ptr = readable (self );
806
- char buf [1 ], * cp = buf ;
807
- long cl = 1 ;
808
806
809
807
check_modifiable (ptr );
810
808
if (NIL_P (c )) return Qnil ;
811
809
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'" );
814
823
}
815
824
else {
825
+ char * cp ;
826
+ long cl ;
816
827
SafeStringValue (c );
817
828
cp = RSTRING_PTR (c );
818
829
cl = RSTRING_LEN (c );
Original file line number Diff line number Diff line change @@ -452,6 +452,10 @@ def test_ungetbyte
452
452
t . ungetbyte ( "\u{30eb 30d3 30fc} " )
453
453
assert_equal ( 0 , t . pos )
454
454
assert_equal ( "\u{30eb 30d3 30fc} \u7d05 \u7389 bar\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 ) }
455
459
end
456
460
457
461
def test_ungetc
You can’t perform that action at this time.
0 commit comments