Skip to content

Commit

Permalink
issues/522 - fix minimum buffer length check
Browse files Browse the repository at this point in the history
  • Loading branch information
cordellcalitz committed Nov 16, 2023
1 parent 0b6a7d8 commit cbded30
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "modbus-serial",
"version": "8.0.13",
"version": "8.0.14",
"description": "A pure JavaScript implemetation of MODBUS-RTU (Serial and TCP) for NodeJS.",
"main": "index.js",
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion servers/serverserial.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion test/Lint/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const paths = [
const options = {
// Specify style of output
formatter: "compact", // Defaults to `stylish`
timeout: 5000
timeout: 10000
};

// Run the tests
Expand Down
10 changes: 10 additions & 0 deletions test/servers/serverserial.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit cbded30

Please sign in to comment.