Skip to content

Commit

Permalink
[modbus] Off-by-one fixes for data thing read index validation (openh…
Browse files Browse the repository at this point in the history
…ab#8301)

Fixes openhab#8300

Signed-off-by: Sami Salonen <[email protected]>
  • Loading branch information
ssalonen authored and markus7017 committed Sep 18, 2020
1 parent 7df74ec commit 2e73959
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ private void validateReadIndex() throws ModbusConfigurationException {

// Determine bit positions polled, both start and end inclusive
int pollStartBitIndex = readRequest.getReference() * dataElementBits;
int pollEndBitIndex = pollStartBitIndex + readRequest.getDataLength() * dataElementBits;
int pollEndBitIndex = pollStartBitIndex + readRequest.getDataLength() * dataElementBits - 1;

// Determine bit positions read, both start and end inclusive
int readStartBitIndex = readIndex.get() * dataElementBits + readSubIndex.orElse(0) * valueTypeBitCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,16 @@ public void testInitCoilsOutOfIndex() {
ThingStatus.OFFLINE);
}

@Test
public void testInitCoilsOutOfIndex2() {
// Reading coils 4, 5, 6. Coil 7 is out of bounds
testOutOfBoundsGeneric(4, 3, "7", ModbusReadFunctionCode.READ_COILS, ModbusConstants.ValueType.BIT,
ThingStatus.OFFLINE);
}

@Test
public void testInitCoilsOK() {
// Reading coils 4, 5, 6. Coil 6 is OK
testOutOfBoundsGeneric(4, 3, "6", ModbusReadFunctionCode.READ_COILS, ModbusConstants.ValueType.BIT,
ThingStatus.ONLINE);
}
Expand Down Expand Up @@ -349,12 +357,20 @@ public void testInitRegistersWithInt8OK2() {

@Test
public void testInitRegistersWithInt16OK() {
// Poller reading registers 4, 5, 6. Register 6 is OK
testOutOfBoundsGeneric(4, 3, "6", ModbusReadFunctionCode.READ_MULTIPLE_REGISTERS,
ModbusConstants.ValueType.INT16, ThingStatus.ONLINE);
}

@Test
public void testInitRegistersWithInt16OutOfBounds() {
// Poller reading registers 4, 5, 6. Register 7 is out-of-bounds
testOutOfBoundsGeneric(4, 3, "7", ModbusReadFunctionCode.READ_MULTIPLE_REGISTERS,
ModbusConstants.ValueType.INT16, ThingStatus.OFFLINE);
}

@Test
public void testInitRegistersWithInt16OutOfBounds2() {
testOutOfBoundsGeneric(4, 3, "8", ModbusReadFunctionCode.READ_MULTIPLE_REGISTERS,
ModbusConstants.ValueType.INT16, ThingStatus.OFFLINE);
}
Expand Down

0 comments on commit 2e73959

Please sign in to comment.