<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1,6 +1,7 @@
 ==
   * Bug Fixes
     * Fix bignum encoding
+    * Prevent stack overflow for massive binaries in c decoder
 
 == 1.1.0 / 2009-10-08
   * Minor Improvements</diff>
      <filename>History.txt</filename>
    </modified>
    <modified>
      <diff>@@ -97,7 +97,7 @@ VALUE read_large_tuple(unsigned char **pData) {
     rb_raise(rb_eStandardError, &quot;Invalid Type, not a large tuple&quot;);
   }
 
-  int arity = read_4(pData);
+  unsigned int arity = read_4(pData);
 
   VALUE array = rb_ary_new2(arity);
 
@@ -114,7 +114,7 @@ VALUE read_list(unsigned char **pData) {
     rb_raise(rb_eStandardError, &quot;Invalid Type, not an erlang list&quot;);
   }
 
-  int size = read_4(pData);
+  unsigned int size = read_4(pData);
 
   VALUE newref_class = rb_const_get(mErlectricity, rb_intern(&quot;List&quot;));
   VALUE array = rb_funcall(newref_class, rb_intern(&quot;new&quot;), 1, INT2NUM(size));
@@ -131,7 +131,7 @@ VALUE read_list(unsigned char **pData) {
 
 // primitives
 
-void read_string_raw(unsigned char *dest, unsigned char **pData, int length) {
+void read_string_raw(unsigned char *dest, unsigned char **pData, unsigned int length) {
   memcpy((char *) dest, (char *) *pData, length);
   *(dest + length) = (unsigned char) 0;
   *pData += length;
@@ -142,12 +142,12 @@ VALUE read_bin(unsigned char **pData) {
     rb_raise(rb_eStandardError, &quot;Invalid Type, not an erlang binary&quot;);
   }
 
-  int length = read_4(pData);
+  unsigned int length = read_4(pData);
 
-  unsigned char buf[length + 1];
-  read_string_raw(buf, pData, length);
+  VALUE rStr = rb_str_new((char *) *pData, length);
+  *pData += length;
 
-  return rb_str_new((char *) buf, length);
+  return rStr;
 }
 
 VALUE read_string(unsigned char **pData) {</diff>
      <filename>ext/decoder.c</filename>
    </modified>
    <modified>
      <diff>@@ -127,6 +127,11 @@ context &quot;When unpacking from a binary stream&quot; do
     get(&quot;f&quot;).should == :f
   end
 
+  specify &quot;massive binaries should not overflow the stack&quot; do
+    bin = [131,109,0,128,0,0].pack('c*') + ('a' * (8 * 1024 * 1024))
+    assert_equal (8 * 1024 * 1024), Erlectricity::Decoder.decode(bin).size
+  end
+
   specify &quot;a good thing should be awesome&quot; do
     get(%Q-[{options,{struct,[{test,&lt;&lt;&quot;I'm chargin' mah lazer&quot;&gt;&gt;}]}},{passage,&lt;&lt;&quot;Why doesn't this work?&quot;&gt;&gt;}]-).should ==
     [[:options, [:struct, [[:test, &quot;I'm chargin' mah lazer&quot;]]]], [:passage, &quot;Why doesn't this work?&quot;]]</diff>
      <filename>test/decode_spec.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>9257ecc222ba5e489ced8d40c6190a4d1aea2826</id>
    </parent>
  </parents>
  <author>
    <name>Tom Preston-Werner</name>
    <email>tom@mojombo.com</email>
  </author>
  <url>http://github.com/mojombo/erlectricity/commit/b3dee26c8738a2ed821d8097c54f8fcda0c50cf5</url>
  <id>b3dee26c8738a2ed821d8097c54f8fcda0c50cf5</id>
  <committed-date>2009-10-28T12:34:41-07:00</committed-date>
  <authored-date>2009-10-28T12:34:41-07:00</authored-date>
  <message>prevent stack overflow for massive binaries in c decoder</message>
  <tree>e421b83c3039043e1fa770d9250d3763fc5b78e0</tree>
  <committer>
    <name>Tom Preston-Werner</name>
    <email>tom@mojombo.com</email>
  </committer>
</commit>
