THRIFT-5769: fix invalid size error on large messages
Client: nodejs
diff --git a/lib/nodejs/lib/thrift/framed_transport.js b/lib/nodejs/lib/thrift/framed_transport.js
index 9a50a73..058d230 100644
--- a/lib/nodejs/lib/thrift/framed_transport.js
+++ b/lib/nodejs/lib/thrift/framed_transport.js
@@ -35,30 +35,27 @@
Object.setPrototypeOf(TFramedTransport.prototype, THeaderTransport.prototype);
TFramedTransport.receiver = function(callback, seqid) {
- var residual = [];
+ var residual = new Buffer(0);
return function(data) {
- // push received data to residual
- for(var i = 0; i < data.length; ++i) {
- residual.push(data[i])
- }
+ residual = Buffer.concat([residual, Buffer.from(data)]);
while (residual.length > 0) {
if (residual.length < 4) {
// Not enough bytes to continue, save and resume on next packet
return;
}
- // get single package sieze
- var frameSize = binary.readI32(Buffer.from(residual.slice(0, 4)), 0);
+ // Get single package size
+ var frameSize = binary.readI32(residual, 0);
// Not enough bytes to continue, save and resume on next packet
if (residual.length < 4 + frameSize) {
return;
}
- // splice first 4 bytes
- residual.splice(0, 4)
- // get package data
- var frame = Buffer.from(residual.splice(0, frameSize));
+ // Get package data
+ var frame = residual.subarray(4, 4 + frameSize);
+ // Remove processed data from residual
+ residual = residual.subarray(4 + frameSize);
callback(new TFramedTransport(frame), seqid);
}
};
diff --git a/lib/nodejs/test/header.test.js b/lib/nodejs/test/header.test.js
index 99bb832..12f1557 100644
--- a/lib/nodejs/test/header.test.js
+++ b/lib/nodejs/test/header.test.js
@@ -100,6 +100,18 @@
assert.equals(otherHeaders.foo, undefined);
assert.equals(otherHeaders.otherfoo, "baz");
assert.end();
+ },
+ "Should handle large messages without crashing": function(assert) {
+ const callback = function() {};
+ const onData = TFramedTransport.receiver(callback);
+
+ const largeChunkSize = 2 * 100 * 1024 * 1024;
+ const largeChunk = Buffer.alloc(largeChunkSize, "A");
+ const sizeBuffer = new Buffer(4);
+ sizeBuffer.writeInt32BE(largeChunkSize + 4, 0);
+ onData(Buffer.concat([sizeBuffer, largeChunk]));
+
+ assert.end();
}
};
diff --git a/lib/nodejs/test/testAll.sh b/lib/nodejs/test/testAll.sh
index 37b6b43..144832e 100755
--- a/lib/nodejs/test/testAll.sh
+++ b/lib/nodejs/test/testAll.sh
@@ -118,6 +118,7 @@
# unit tests
node ${DIR}/binary.test.js || TESTOK=1
+node ${DIR}/header.test.js || TESTOK=1
node ${DIR}/int64.test.js || TESTOK=1
node ${DIR}/deep-constructor.test.js || TESTOK=1