[前][次][番号順一覧][スレッド一覧]

ruby-changes:54545

From: kazu <ko1@a...>
Date: Wed, 9 Jan 2019 21:55:26 +0900 (JST)
Subject: [ruby-changes:54545] kazu:r66760 (trunk): Follow behaviour of IO#ungetbyte

kazu	2019-01-09 21:55:20 +0900 (Wed, 09 Jan 2019)

  New Revision: 66760

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66760

  Log:
    Follow behaviour of IO#ungetbyte
    
    see r65802 and [Bug #14359]

  Modified files:
    trunk/ext/stringio/stringio.c
    trunk/test/stringio/test_stringio.rb
Index: ext/stringio/stringio.c
===================================================================
--- ext/stringio/stringio.c	(revision 66759)
+++ ext/stringio/stringio.c	(revision 66760)
@@ -803,16 +803,27 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L803
 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);
Index: test/stringio/test_stringio.rb
===================================================================
--- test/stringio/test_stringio.rb	(revision 66759)
+++ test/stringio/test_stringio.rb	(revision 66760)
@@ -452,6 +452,10 @@ class TestStringIO < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/stringio/test_stringio.rb#L452
     t.ungetbyte("\u{30eb 30d3 30fc}")
     assert_equal(0, t.pos)
     assert_equal("\u{30eb 30d3 30fc}\u7d05\u7389bar\n", s)
+
+    assert_raise(RangeError) {t.ungetbyte(-1)}
+    assert_raise(RangeError) {t.ungetbyte(256)}
+    assert_raise(RangeError) {t.ungetbyte(1<<64)}
   end
 
   def test_ungetc

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]