From 1b33c7c812df746fffc081720eca568fddc845d4 Mon Sep 17 00:00:00 2001 From: cordellcalitz <2901443+cordellcalitz@users.noreply.github.com> Date: Fri, 17 Nov 2023 09:20:17 +0200 Subject: [PATCH] issues/522 - fix minimum buffer length check (#523) * issues/522 - fix minimum buffer length check * issues/522 - roll back version spin --- servers/serverserial.js | 3 ++- test/Lint/test.js | 2 +- test/servers/serverserial.test.js | 10 ++++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/servers/serverserial.js b/servers/serverserial.js index 9140f45..81ca1c1 100644 --- a/servers/serverserial.js +++ b/servers/serverserial.js @@ -26,6 +26,7 @@ const BAUDRATE = 9600; const UNIT_ID = 255; // listen to all adresses const ADDR_LEN = 1; +const MIN_LEN = 6; /* Get Handlers */ @@ -124,7 +125,7 @@ function _callbackFactory(unitID, functionCode, sockWriter) { */ function _parseModbusBuffer(requestBuffer, vector, serverUnitID, sockWriter, options) { // Check requestBuffer length - if (!requestBuffer || requestBuffer.length < ADDR_LEN) { + if (!requestBuffer || requestBuffer.length < MIN_LEN) { modbusSerialDebug("wrong size of request Buffer " + requestBuffer.length); return; } diff --git a/test/Lint/test.js b/test/Lint/test.js index a0a28cf..534b2df 100644 --- a/test/Lint/test.js +++ b/test/Lint/test.js @@ -16,7 +16,7 @@ const paths = [ const options = { // Specify style of output formatter: "compact", // Defaults to `stylish` - timeout: 5000 + timeout: 10000 }; // Run the tests diff --git a/test/servers/serverserial.test.js b/test/servers/serverserial.test.js index a081f88..ef369f2 100644 --- a/test/servers/serverserial.test.js +++ b/test/servers/serverserial.test.js @@ -229,6 +229,16 @@ describe("Modbus Serial Server (no serverID)", function() { // TODO: exceptions }); + + describe("too short client request", function() { + it("should handle a request that is too short without crash", function(done) { + // valid ID, function code & CRC, but too short body + serverSerial.getPort().write(Buffer.from("081013bc0f", "hex")); + + // wait a bit to make sure we didn't crash + setTimeout(done, 50); + }); + }); }); describe("Modbus Serial Server (serverID = requestID)", function() {