Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issues/522 - fix minimum buffer length check #523

Merged
merged 2 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
yaacov marked this conversation as resolved.
Show resolved Hide resolved
"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
yaacov marked this conversation as resolved.
Show resolved Hide resolved
};

// 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
Loading