Skip to content

Commit

Permalink
Merge pull request #128 from facchinm/update_libmodbus
Browse files Browse the repository at this point in the history
Update libmodbus
  • Loading branch information
facchinm authored Oct 27, 2023
2 parents 0cee633 + e19543e commit d6eaa36
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/libmodbus/modbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -897,9 +897,10 @@ int modbus_reply(modbus_t *ctx, const uint8_t *req,
break;
case MODBUS_FC_WRITE_MULTIPLE_COILS: {
int nb = (req[offset + 3] << 8) + req[offset + 4];
int nb_bits = req[offset + 5];
int mapping_address = address - mb_mapping->start_bits;

if (nb < 1 || MODBUS_MAX_WRITE_BITS < nb) {
if (nb < 1 || MODBUS_MAX_WRITE_BITS < nb || nb_bits * 8 < nb) {
/* May be the indication has been truncated on reading because of
* invalid address (eg. nb is 0 but the request contains values to
* write) so it's necessary to flush. */
Expand Down Expand Up @@ -928,9 +929,10 @@ int modbus_reply(modbus_t *ctx, const uint8_t *req,
break;
case MODBUS_FC_WRITE_MULTIPLE_REGISTERS: {
int nb = (req[offset + 3] << 8) + req[offset + 4];
int nb_bytes = req[offset + 5];
int mapping_address = address - mb_mapping->start_registers;

if (nb < 1 || MODBUS_MAX_WRITE_REGISTERS < nb) {
if (nb < 1 || MODBUS_MAX_WRITE_REGISTERS < nb || nb_bytes * 8 < nb) {
rsp_length = response_exception(
ctx, &sft, MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE, rsp, TRUE,
"Illegal number of values %d in write_registers (max %d)\n",
Expand Down Expand Up @@ -1017,7 +1019,7 @@ int modbus_reply(modbus_t *ctx, const uint8_t *req,
nb_write, nb, MODBUS_MAX_WR_WRITE_REGISTERS, MODBUS_MAX_WR_READ_REGISTERS);
} else if (mapping_address < 0 ||
(mapping_address + nb) > mb_mapping->nb_registers ||
mapping_address < 0 ||
mapping_address_write < 0 ||
(mapping_address_write + nb_write) > mb_mapping->nb_registers) {
rsp_length = response_exception(
ctx, &sft, MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS, rsp, FALSE,
Expand Down

0 comments on commit d6eaa36

Please sign in to comment.