From 10b4528b4d2de2a1df41f34c5f6bfc88f4520d15 Mon Sep 17 00:00:00 2001 From: Aidan Cyr Date: Fri, 9 Aug 2024 07:13:00 +1000 Subject: [PATCH 1/2] Fix some invalid decoded lengths from various --- src/BERDecode.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/BERDecode.cpp b/src/BERDecode.cpp index 9ecfe28..229a535 100644 --- a/src/BERDecode.cpp +++ b/src/BERDecode.cpp @@ -133,7 +133,7 @@ int OIDType::fromBuffer(const uint8_t *buf, size_t max_len){ this->data.assign(dataPtr, dataPtr + _length); this->valid = true; - return _length + 2; + return _length + j; } static inline void long_to_buf(char* buf, long l, short r = 0){ @@ -208,7 +208,7 @@ int Counter64::fromBuffer(const uint8_t *buf, size_t max_len){ _value = _value | *ptr++; tempLength--; } - return _length + 2; + return _length + i; } std::shared_ptr ComplexType::createObjectForType(ASN_TYPE valueType){ @@ -290,5 +290,5 @@ int ComplexType::fromBuffer(const uint8_t *buf, size_t max_len){ ptr += used_length; i += used_length; } - return _length + 2; + return _length + j; } \ No newline at end of file From 0b2c7ebae2761ec27020ca6bf001669cc49da03d Mon Sep 17 00:00:00 2001 From: N1IOX Date: Sun, 11 Jan 2026 22:52:21 -0500 Subject: [PATCH 2/2] length values >= 256 need 3 bytes to encode their length a length of exactly 256 should be encoded as: 0x82 0x01 0x00 without this change, I was seeing the encoded length of a get response (that happened to be exactly 256 bytes long) encoded as: 0x81 0x00 that was causing snmpbulkwalk to fail, since it was reading the encoded length as 0. --- src/BEREncode.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BEREncode.cpp b/src/BEREncode.cpp index 954a4ca..fed7d8c 100644 --- a/src/BEREncode.cpp +++ b/src/BEREncode.cpp @@ -21,7 +21,7 @@ static size_t encode_ber_length_integer(uint8_t* buf, size_t integer, int){ if(integer < 128){ *buf = integer & 0xFF; } else { - if(integer > 256){ + if(integer >= 256){ *buf++ = (2 | 0x80) & 0xFF; *buf++ = integer/256; bytes_used += 2; @@ -37,7 +37,7 @@ static size_t encode_ber_length_integer(uint8_t* buf, size_t integer, int){ static size_t encode_ber_length_integer_count(size_t integer){ int bytes_used = 1; if(integer >= 128){ - if(integer > 256){ + if(integer >= 256){ bytes_used += 2; } else { bytes_used++;