@@ -897,9 +897,10 @@ int modbus_reply(modbus_t *ctx, const uint8_t *req,
897
897
break ;
898
898
case MODBUS_FC_WRITE_MULTIPLE_COILS : {
899
899
int nb = (req [offset + 3 ] << 8 ) + req [offset + 4 ];
900
+ int nb_bits = req [offset + 5 ];
900
901
int mapping_address = address - mb_mapping -> start_bits ;
901
902
902
- if (nb < 1 || MODBUS_MAX_WRITE_BITS < nb ) {
903
+ if (nb < 1 || MODBUS_MAX_WRITE_BITS < nb || nb_bits * 8 < nb ) {
903
904
/* May be the indication has been truncated on reading because of
904
905
* invalid address (eg. nb is 0 but the request contains values to
905
906
* write) so it's necessary to flush. */
@@ -928,9 +929,10 @@ int modbus_reply(modbus_t *ctx, const uint8_t *req,
928
929
break ;
929
930
case MODBUS_FC_WRITE_MULTIPLE_REGISTERS : {
930
931
int nb = (req [offset + 3 ] << 8 ) + req [offset + 4 ];
932
+ int nb_bytes = req [offset + 5 ];
931
933
int mapping_address = address - mb_mapping -> start_registers ;
932
934
933
- if (nb < 1 || MODBUS_MAX_WRITE_REGISTERS < nb ) {
935
+ if (nb < 1 || MODBUS_MAX_WRITE_REGISTERS < nb || nb_bytes * 8 < nb ) {
934
936
rsp_length = response_exception (
935
937
ctx , & sft , MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE , rsp , TRUE,
936
938
"Illegal number of values %d in write_registers (max %d)\n" ,
@@ -1017,7 +1019,7 @@ int modbus_reply(modbus_t *ctx, const uint8_t *req,
1017
1019
nb_write , nb , MODBUS_MAX_WR_WRITE_REGISTERS , MODBUS_MAX_WR_READ_REGISTERS );
1018
1020
} else if (mapping_address < 0 ||
1019
1021
(mapping_address + nb ) > mb_mapping -> nb_registers ||
1020
- mapping_address < 0 ||
1022
+ mapping_address_write < 0 ||
1021
1023
(mapping_address_write + nb_write ) > mb_mapping -> nb_registers ) {
1022
1024
rsp_length = response_exception (
1023
1025
ctx , & sft , MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS , rsp , FALSE,
0 commit comments