Skip to content

Commit

Permalink
issues/522 - fix minimum buffer length check (#523)
Browse files Browse the repository at this point in the history
* issues/522 - fix minimum buffer length check

* issues/522 - roll back version spin
  • Loading branch information
cordellcalitz authored Nov 17, 2023
1 parent 0b6a7d8 commit 1b33c7c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
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 1b33c7c

Please sign in to comment.