You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by kc...@apache.org on 2008/06/18 03:08:19 UTC

svn commit: r668955 - in /incubator/thrift/trunk/lib/rb: lib/thrift/protocol/binaryprotocol.rb spec/binaryprotocol_spec.rb

Author: kclark
Date: Tue Jun 17 18:08:19 2008
New Revision: 668955

URL: http://svn.apache.org/viewvc?rev=668955&view=rev
Log:
Stop range-checking in BinaryProtocol#write_iNN, it turns out to break some code

Modified:
    incubator/thrift/trunk/lib/rb/lib/thrift/protocol/binaryprotocol.rb
    incubator/thrift/trunk/lib/rb/spec/binaryprotocol_spec.rb

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/protocol/binaryprotocol.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/protocol/binaryprotocol.rb?rev=668955&r1=668954&r2=668955&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/protocol/binaryprotocol.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/protocol/binaryprotocol.rb Tue Jun 17 18:08:19 2008
@@ -54,23 +54,18 @@
     end
 
     def write_byte(byte)
-      # yes, -128..255. This covers signed byte min -> unsigned byte max
-      raise RangeError.new("#{byte} too large to fit in a byte") unless (-128..127).include? byte
       trans.write([byte].pack('n')[1..1])
     end
 
     def write_i16(i16)
-      raise RangeError.new("#{i16} too large to fit in an i16") unless ((-2**15)..(2**15-1)).include? i16
       trans.write([i16].pack('n'))
     end
 
     def write_i32(i32)
-      raise RangeError.new("#{i32} too large to fit in an i32") unless ((-2**31)..(2**31-1)).include? i32
       trans.write([i32].pack('N'))
     end
 
     def write_i64(i64)
-      raise RangeError.new("#{i64} too large to fit in an i32") unless ((-2**63)..(2**63-1)).include? i64
       hi = i64 >> 32
       lo = i64 & 0xffffffff
       trans.write([hi, lo].pack('N2'))

Modified: incubator/thrift/trunk/lib/rb/spec/binaryprotocol_spec.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/spec/binaryprotocol_spec.rb?rev=668955&r1=668954&r2=668955&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/binaryprotocol_spec.rb (original)
+++ incubator/thrift/trunk/lib/rb/spec/binaryprotocol_spec.rb Tue Jun 17 18:08:19 2008
@@ -71,12 +71,19 @@
       # byte is small enough, let's check -128..127
       (-128..127).each do |i|
         @trans.should_receive(:write).with([i].pack('c')).ordered
+        @prot.write_byte(i)
       end
       (-128..127).each do |i|
+      end
+      # handing it numbers out of signed range should clip
+      @trans.rspec_verify
+      @trans.rspec_clear
+      (128..255).each do |i|
+        @trans.should_receive(:write).with([i].pack('c')).ordered
         @prot.write_byte(i)
       end
-      # now try out of signed range
-      lambda { @prot.write_byte(224) }.should raise_error(RangeError)
+      # and lastly, a Bignum is going to error out
+      lambda { @prot.write_byte(2**65) }.should raise_error(RangeError)
     end
 
     it "should write an i16" do
@@ -92,8 +99,11 @@
       [-2**15, -1024, 17, 0, -10000, 1723, 2**15-1].each do |i|
         @prot.write_i16(i)
       end
-      # and try something out of signed range
-      lambda { @prot.write_i16(2**15 + 5) }.should raise_error(RangeError)
+      # and try something out of signed range, it should clip
+      @trans.should_receive(:write).with("\200\005").ordered
+      @prot.write_i16(2**15 + 5)
+      # a Bignum should error
+      lambda { @prot.write_i16(2**65) }.should raise_error(RangeError)
     end
 
     it "should write an i32" do
@@ -110,8 +120,10 @@
       [-2**31, -123123, -2532, -3, 0, 2351235, 12331, 2**31-1].each do |i|
         @prot.write_i32(i)
       end
-      # try something out of signed range
-      lambda { @prot.write_i32(2 ** 31 + 5) }.should raise_error(RangeError)
+      # try something out of signed range, it should clip
+      @trans.should_receive(:write).with("\200\000\000\005").ordered
+      @prot.write_i32(2 ** 31 + 5)
+      lambda { @prot.write_i32(2 ** 65 + 5) }.should raise_error(RangeError)
     end
 
     it "should write an i64" do
@@ -129,8 +141,10 @@
       [-2**63, -12356123612323, -23512351, -234, 0, 1231, 2351236, 12361236213, 2**63-1].each do |i|
         @prot.write_i64(i)
       end
-      # try something out of signed range
-      lambda { @prot.write_i64(2 ** 63 + 5) }.should raise_error(RangeError)
+      # try something out of signed range, it should clip
+      @trans.should_receive(:write).with("\200\000\000\000\000\000\000\005").ordered
+      @prot.write_i64(2**63 + 5)
+      lambda { @prot.write_i64(2 ** 65 + 5) }.should raise_error(RangeError)
     end
 
     it "should write a double" do