fix: correct buffers concatenation in socket (#787)

This commit is contained in:
Simon Hamelin
2024-08-19 16:46:03 -04:00
committed by GitHub
parent 2a0d29c1f5
commit cc2c24f206

View File

@@ -126,15 +126,18 @@ class ClientRequestManager {
tlsSocket.on('secureConnect', () => { tlsSocket.on('secureConnect', () => {
tlsSocket.on('data', (chunk: Buffer) => { tlsSocket.on('data', (chunk: Buffer) => {
if (rpcBuffer != null) { if (rpcBuffer != null) {
rpcBuffer = Buffer.concat([rpcBuffer, chunk], rpcBufferSize); rpcBuffer = Buffer.concat(
[rpcBuffer, chunk],
rpcBufferSize <= rpcBuffer.length + chunk.length ? rpcBufferSize : undefined,
);
} else { } else {
if (chunk[0] !== DELUGE_RPC_PROTOCOL_VERSION) { if (chunk[0] !== DELUGE_RPC_PROTOCOL_VERSION) {
handleError(new Error('Unexpected Deluge RPC version.')); handleError(new Error('Unexpected Deluge RPC version.'));
return; return;
} }
rpcBufferSize = chunk.slice(1, 5).readUInt32BE(0); rpcBufferSize = chunk.subarray(1, 5).readUInt32BE(0);
rpcBuffer = chunk.slice(5); rpcBuffer = chunk.subarray(5);
} }
if (rpcBuffer.length >= rpcBufferSize) { if (rpcBuffer.length >= rpcBufferSize) {