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

[modbus] Off-by-one fixes for data thing read index validation #8301

Merged
merged 1 commit into from
Aug 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
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