0neblock / Arduino_SNMP

SNMP Agent built with Arduino
MIT License
77 stars 30 forks source link

Long Form Length bug -- fix included #46

Closed doc-aka-rich closed 1 month ago

doc-aka-rich commented 6 months ago

When this agent receives an SNMP message encoded using long form length fields (header & length longer than 2 bytes) there is an issue with the following function in the BERDecode.cpp file.

int ComplexType::fromBuffer(const uint8_t *buf, size_t max_len){}

this is a recursive routine that calls itself on each new SNMP 'STRUCTURE' message type.

When it reaches the end of said SNMP 'STRUCTURE' message segment type it returns (to itself) returning the length used by the 'STRUCTURE' segment with a return line:

return _length + 2;

It adds '2' to the _length of the STRUCTURE segment presuming the header & length field is only 2 bytes (short form lengths). With an SNMP message using long form length fields the header & length is 4 bytes or longer, resulting in the recursive function incorrectly pointing a few bytes from the end of the last STRUCTURE segment instead of advancing correctly beyond the just parsed segment.

Every time this routine is entered on it's first line assigns the length of the header & length to a local 'j'. Modifying the last line in this function to return

return _length + j;

instead of

return _length + 2:

appears to cure the issue and makes the SNMP_Agent work with SNMP messages encoded using long form length fields.

0neblock commented 1 month ago

Thanks for the suggestion, I've applied the fix here: https://github.com/0neblock/Arduino_SNMP/pull/49