Skip to content

Commit

Permalink
Fix packstream list bug for lists > 255
Browse files Browse the repository at this point in the history
  • Loading branch information
oskarhane committed Feb 2, 2016
1 parent 795de56 commit 44048d7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/v1/internal/packstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class Packer {
this._ch.writeBytes(bytes);
} else if (size < 0x100000000) {
this._ch.writeUInt8(STRING_32);
this._ch.writeUInt8((size/16777216>>0)%256); // TODO: Why is it shifting by 0 here?
this._ch.writeUInt8((size/16777216>>0)%256);
this._ch.writeUInt8((size/65536>>0)%256);
this._ch.writeUInt8((size/256>>0)%256);
this._ch.writeUInt8(size%256);
Expand All @@ -188,7 +188,9 @@ class Packer {
this._ch.writeUInt8(LIST_8)
this._ch.writeUInt8(size);
} else if (size < 0x10000) {
this._ch.writeUInt8(LIST_16, size/256>>0, size%256);
this._ch.writeUInt8(LIST_16);
this._ch.writeUInt8((size/256>>0)%256);
this._ch.writeUInt8(size%256);
} else if (size < 0x100000000) {
this._ch.writeUInt8(LIST_32);
this._ch.writeUInt8((size/16777216>>0)%256);
Expand Down
16 changes: 14 additions & 2 deletions test/internal/packstream.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,22 @@ describe('packstream', function() {
expect( roundtripped[0] ).toBe( list[0] );
expect( roundtripped[1] ).toBe( list[1] );
});

it('should pack long lists', function() {
var listLength = 256;
var list = [];
for(var i = 0; i < listLength; i++) {
list.push(null)
}
var roundtripped = packAndUnpack( list, 1400 );
expect( roundtripped[0] ).toBe( list[0] );
expect( roundtripped[1] ).toBe( list[1] );
});
});

function packAndUnpack( val ) {
var buffer = alloc(128);
function packAndUnpack( val, bufferSize ) {
bufferSize = bufferSize || 128;
var buffer = alloc(bufferSize);
new Packer( buffer ).pack( val );
buffer.reset();
return new Unpacker().unpack( buffer );
Expand Down

0 comments on commit 44048d7

Please sign in to comment.