You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by dr...@apache.org on 2008/07/25 23:06:06 UTC

svn commit: r679899 - in /incubator/thrift/trunk/lib/rb: ext/binaryprotocolaccelerated.c spec/binaryprotocolaccelerated_spec.rb

Author: dreiss
Date: Fri Jul 25 14:06:06 2008
New Revision: 679899

URL: http://svn.apache.org/viewvc?rev=679899&view=rev
Log:
rb: Teach BinaryProtocolAccelerated to encode strings with NULs [THRIFT-97]

This patch adds a spec and fixes the behavior to properly treat strings as binary
blobs of data instead of as C strings when writing to the transport.

Modified:
    incubator/thrift/trunk/lib/rb/ext/binaryprotocolaccelerated.c
    incubator/thrift/trunk/lib/rb/spec/binaryprotocolaccelerated_spec.rb

Modified: incubator/thrift/trunk/lib/rb/ext/binaryprotocolaccelerated.c
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/ext/binaryprotocolaccelerated.c?rev=679899&r1=679898&r2=679899&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/ext/binaryprotocolaccelerated.c (original)
+++ incubator/thrift/trunk/lib/rb/ext/binaryprotocolaccelerated.c Fri Jul 25 14:06:06 2008
@@ -259,10 +259,9 @@
   write_i64(buf, transfer.t);
 }
 
-static void write_string(VALUE buf, char* str) {
-  int32_t len = strlen(str);
+static void write_string(VALUE buf, char* str, size_t len) {
   write_i32(buf, len);
-  rb_str_buf_cat2(buf, str);
+  rb_str_buf_cat(buf, str, len);
 }
 
 // Some functions macro'd out because they're nops for the binary protocol
@@ -473,7 +472,7 @@
       break;
 
     case T_STR:
-      write_string(buf, StringValuePtr(obj));
+      write_string(buf, StringValuePtr(obj), RSTRING(obj)->len);
       break;
           
     case T_STRCT: {

Modified: incubator/thrift/trunk/lib/rb/spec/binaryprotocolaccelerated_spec.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/spec/binaryprotocolaccelerated_spec.rb?rev=679899&r1=679898&r2=679899&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/binaryprotocolaccelerated_spec.rb (original)
+++ incubator/thrift/trunk/lib/rb/spec/binaryprotocolaccelerated_spec.rb Fri Jul 25 14:06:06 2008
@@ -70,6 +70,16 @@
 \000\005words\f\000\003\v\000\001\000\000\000\rhello, world!\000\000")
       @prot.decode_binary(SpecNamespace::Foo.new, trans).should == SpecNamespace::Foo.new
     end
+
+    it "should encode a string with null bytes in it" do
+      foo = SpecNamespace::Hello.new(:greeting => "Hello\000World!")
+      @prot.encode_binary(foo).should == "\v\000\001\000\000\000\fHello\000World!\000"
+    end
+
+    it "should decode a string with null bytes in it" do
+      trans = Thrift::MemoryBuffer.new("\v\000\001\000\000\000\fHello\000World!\000")
+      @prot.decode_binary(SpecNamespace::Hello.new, trans).should == SpecNamespace::Hello.new(:greeting => "Hello\000World!")
+    end
   end
 
   describe BinaryProtocolAcceleratedFactory do