Skip to content

Commit 1b33c7c

Browse files
issues/522 - fix minimum buffer length check (#523)
* issues/522 - fix minimum buffer length check * issues/522 - roll back version spin
1 parent 0b6a7d8 commit 1b33c7c

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

servers/serverserial.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const BAUDRATE = 9600;
2626
const UNIT_ID = 255; // listen to all adresses
2727

2828
const ADDR_LEN = 1;
29+
const MIN_LEN = 6;
2930

3031
/* Get Handlers
3132
*/
@@ -124,7 +125,7 @@ function _callbackFactory(unitID, functionCode, sockWriter) {
124125
*/
125126
function _parseModbusBuffer(requestBuffer, vector, serverUnitID, sockWriter, options) {
126127
// Check requestBuffer length
127-
if (!requestBuffer || requestBuffer.length < ADDR_LEN) {
128+
if (!requestBuffer || requestBuffer.length < MIN_LEN) {
128129
modbusSerialDebug("wrong size of request Buffer " + requestBuffer.length);
129130
return;
130131
}

test/Lint/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const paths = [
1616
const options = {
1717
// Specify style of output
1818
formatter: "compact", // Defaults to `stylish`
19-
timeout: 5000
19+
timeout: 10000
2020
};
2121

2222
// Run the tests

test/servers/serverserial.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,16 @@ describe("Modbus Serial Server (no serverID)", function() {
229229

230230
// TODO: exceptions
231231
});
232+
233+
describe("too short client request", function() {
234+
it("should handle a request that is too short without crash", function(done) {
235+
// valid ID, function code & CRC, but too short body
236+
serverSerial.getPort().write(Buffer.from("081013bc0f", "hex"));
237+
238+
// wait a bit to make sure we didn't crash
239+
setTimeout(done, 50);
240+
});
241+
});
232242
});
233243

234244
describe("Modbus Serial Server (serverID = requestID)", function() {

0 commit comments

Comments
 (0)