@@ -832,8 +832,9 @@ int modbus_reply(modbus_t *ctx, const uint8_t *req,
832
832
break ;
833
833
case _FC_WRITE_MULTIPLE_COILS : {
834
834
int nb = (req [offset + 3 ] << 8 ) + req [offset + 4 ];
835
+ int nb_bits = req [offset + 5 ];
835
836
836
- if (nb < 1 || MODBUS_MAX_WRITE_BITS < nb ) {
837
+ if (nb < 1 || MODBUS_MAX_WRITE_BITS < nb || nb_bits * 8 < nb ) {
837
838
if (ctx -> debug ) {
838
839
fprintf (stderr ,
839
840
"Illegal number of values %d in write_bits (max %d)\n" ,
@@ -842,7 +843,7 @@ int modbus_reply(modbus_t *ctx, const uint8_t *req,
842
843
rsp_length = response_exception (
843
844
ctx , & sft ,
844
845
MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE , rsp );
845
- } else if ((address + nb ) > mb_mapping -> nb_bits ) {
846
+ } else if ((address + nb ) > mb_mapping -> nb_bits ) {
846
847
if (ctx -> debug ) {
847
848
fprintf (stderr , "Illegal data address %0X in write_bits\n" ,
848
849
address + nb );
@@ -863,8 +864,9 @@ int modbus_reply(modbus_t *ctx, const uint8_t *req,
863
864
break ;
864
865
case _FC_WRITE_MULTIPLE_REGISTERS : {
865
866
int nb = (req [offset + 3 ] << 8 ) + req [offset + 4 ];
867
+ int nb_bytes = req [offset + 5 ];
866
868
867
- if (nb < 1 || MODBUS_MAX_WRITE_REGISTERS < nb ) {
869
+ if (nb < 1 || MODBUS_MAX_WRITE_REGISTERS < nb || nb_bytes * 8 < nb ) {
868
870
if (ctx -> debug ) {
869
871
fprintf (stderr ,
870
872
"Illegal number of values %d in write_registers (max %d)\n" ,
0 commit comments