coap_opt_size underestimates space usage
Status: Beta
Brought to you by:
obgm
when responding to a message containing a block2 option when it is not whitelisted, a "WARN coap_add_data: cannot add: data too large for PDU" comes out of the coap_new_error_response function. i've traced it down in the debugger to COAP_OPT_SIZE adding 2 bytes to the total length, while 3 bytes get finally added to the message.
a gdb session log (attached) shows where things go wrong: between $14 and $15 (in the per-option size-adding loop), size grows by 2, but between $20 and $21, length grows by 3.
i'll try to set up a minimal example to find the bug more easily.
@minimal example: the coap-server example shows the problem when requesting /time with block2 set. (tested with copper client configured to coap-13, block2=0).
Thanks for tracking this down. I will have a look at this.
I think this is an example of a more general problem in coap_new_error_response(), when it is compiled to add text to the response payload. The problem is that the payload calculation only reflects the size of the error response string, and does not account for the one byte payload marker that will also be required. In my testing I found that all error responses provoked this same "data too large for PDU", and the debugger showed that all were just one byte short.
My fix was to change the initial payload size calculation to this:
- Tom
Thanks again for the reminder. This one was a bit tricky. The fix is in commit 095e2ee629b5176eff7044ed431587e6c0b1cbbf.