From 10b4528b4d2de2a1df41f34c5f6bfc88f4520d15 Mon Sep 17 00:00:00 2001 From: Aidan Cyr Date: Fri, 9 Aug 2024 07:13:00 +1000 Subject: [PATCH 1/5] 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/5] 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++; From 7060d3adba75662e58f56c356ffb086d00a41f56 Mon Sep 17 00:00:00 2001 From: N1IOX Date: Tue, 13 Jan 2026 18:53:38 -0500 Subject: [PATCH 3/5] use sizeof --- tests/tests.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/tests.cpp b/tests/tests.cpp index b482c5a..7dec46f 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -65,7 +65,7 @@ TEST_CASE( "Test handle failures when Encoding/Decoding", "[snmp]"){ char old[10] = {0}; memcpy(old, &buffer[i], 10); long randomLong = random(); - memcpy(&buffer[i], &randomLong, 10); + memcpy(&buffer[i], &randomLong, sizeof(randomLong)); // This may SOMETIMES fail if the random gets lucky and makes something valid REQUIRE( readPacket->parseFrom(buffer, 200) != SNMP_ERROR_OK ); From 19121fd72e6d331c307591f0a9626a6048d01727 Mon Sep 17 00:00:00 2001 From: N1IOX Date: Tue, 13 Jan 2026 19:39:14 -0500 Subject: [PATCH 4/5] =?UTF-8?q?fix=20a=20compiler=20warning=20treated=20as?= =?UTF-8?q?=20an=20error:=20"operation=20on=20=E2=80=98tempVal=E2=80=99=20?= =?UTF-8?q?may=20be=20undefined"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BERDecode.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BERDecode.cpp b/src/BERDecode.cpp index 229a535..5f041d7 100644 --- a/src/BERDecode.cpp +++ b/src/BERDecode.cpp @@ -89,7 +89,7 @@ int IntegerType::fromBuffer(const uint8_t *buf, size_t max_len){ break; case 3: if(tempVal & 0x00800000){ - tempVal = tempVal |= 0xFF000000; + tempVal |= 0xFF000000; } _value = (int32_t)tempVal; break; From 72ea18a56828b32addeb0bd68e22a1429753b335 Mon Sep 17 00:00:00 2001 From: N1IOX Date: Wed, 14 Jan 2026 20:20:40 -0500 Subject: [PATCH 5/5] As documented in the comments of PR 59 ( https://github.com/0neblock/Arduino_SNMP/pull/59 ), one of the unit test cases has an issue, resulting in false negatives. This then prevents the automated workflow from succeeding. ***Temporarily*** disable the one unit test: "Should fail to parse a corrupt buffer" This test needs to be modified to ensure that it always results in the intended test outcome, perhaps by not relying on random data. --- tests/tests.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/tests.cpp b/tests/tests.cpp index 7dec46f..c62ba53 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -59,7 +59,7 @@ TEST_CASE( "Test handle failures when Encoding/Decoding", "[snmp]"){ REQUIRE( readPack->parseFrom(buffer, 133) == SNMP_ERROR_OK ); } - SECTION( "Should fail to parse a corrupt buffer "){ +/* SECTION( "Should fail to parse a corrupt buffer "){ SNMPPacket* readPacket = new SNMPPacket(); for(int i = 25; i < 133; i+= 10){ char old[10] = {0}; @@ -72,7 +72,7 @@ TEST_CASE( "Test handle failures when Encoding/Decoding", "[snmp]"){ memcpy(&buffer[i], old, 10); REQUIRE( readPacket->parseFrom(buffer, 200) == SNMP_ERROR_OK ); } - } + } */ } TEST_CASE( "Test Encoding/Decoding packet", "[snmp]" ) {