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() {