debevv / nanoMODBUS

A compact MODBUS RTU/TCP C library for embedded/microcontrollers
MIT License
234 stars 47 forks source link

Out of bounds access in Arduino server example #39

Closed Fehlerteufel31 closed 4 months ago

Fehlerteufel31 commented 8 months ago

Hi,

I don't understand why there is +1 in this if statement. It looks to me like the following array server_registers can be accessed out of bounds if quantity = REGS_ADDR_MAX+1 https://github.com/debevv/nanoMODBUS/blob/0642bbb1dee018b8df5400dce9e38e91028964ec/examples/arduino/server-rtu/server-rtu.ino#L73-L80

debevv commented 7 months ago

Hi, sorry for replying two months later. I wrote that code a long ago, so I'm not entirely sure, but I think it should be correct because REGS_ADDR_MAX is the maximum addressable data address, so for example if the client requested 3 registers starting from address 0 and our REGS_ADDR_MAX is 2 (so we expose addresses 0,1 and 2), the request is valid having quantity = REGS_ADDR_MAX+1