Skip to content

Commit eed3ca0

Browse files
N1IOX0neblock
andauthored
Fix some BER decoding (#60)
* Fix some invalid decoded lengths from various * 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. * use sizeof * fix a compiler warning treated as an error: "operation on ‘tempVal’ may be undefined" * As documented in the comments of PR 59 ( #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. --------- Co-authored-by: Aidan Cyr <aidan@aidancyr.dev>
1 parent 7ae4c66 commit eed3ca0

3 files changed

Lines changed: 9 additions & 9 deletions

File tree

src/BERDecode.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ int IntegerType::fromBuffer(const uint8_t *buf, size_t max_len){
8989
break;
9090
case 3:
9191
if(tempVal & 0x00800000){
92-
tempVal = tempVal |= 0xFF000000;
92+
tempVal |= 0xFF000000;
9393
}
9494
_value = (int32_t)tempVal;
9595
break;
@@ -133,7 +133,7 @@ int OIDType::fromBuffer(const uint8_t *buf, size_t max_len){
133133
this->data.assign(dataPtr, dataPtr + _length);
134134
this->valid = true;
135135

136-
return _length + 2;
136+
return _length + j;
137137
}
138138

139139
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){
208208
_value = _value | *ptr++;
209209
tempLength--;
210210
}
211-
return _length + 2;
211+
return _length + i;
212212
}
213213

214214
std::shared_ptr<BER_CONTAINER> ComplexType::createObjectForType(ASN_TYPE valueType){
@@ -290,5 +290,5 @@ int ComplexType::fromBuffer(const uint8_t *buf, size_t max_len){
290290
ptr += used_length;
291291
i += used_length;
292292
}
293-
return _length + 2;
293+
return _length + j;
294294
}

src/BEREncode.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static size_t encode_ber_length_integer(uint8_t* buf, size_t integer, int){
2121
if(integer < 128){
2222
*buf = integer & 0xFF;
2323
} else {
24-
if(integer > 256){
24+
if(integer >= 256){
2525
*buf++ = (2 | 0x80) & 0xFF;
2626
*buf++ = integer/256;
2727
bytes_used += 2;
@@ -37,7 +37,7 @@ static size_t encode_ber_length_integer(uint8_t* buf, size_t integer, int){
3737
static size_t encode_ber_length_integer_count(size_t integer){
3838
int bytes_used = 1;
3939
if(integer >= 128){
40-
if(integer > 256){
40+
if(integer >= 256){
4141
bytes_used += 2;
4242
} else {
4343
bytes_used++;

tests/tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,20 @@ TEST_CASE( "Test handle failures when Encoding/Decoding", "[snmp]"){
5959
REQUIRE( readPack->parseFrom(buffer, 133) == SNMP_ERROR_OK );
6060
}
6161

62-
SECTION( "Should fail to parse a corrupt buffer "){
62+
/* SECTION( "Should fail to parse a corrupt buffer "){
6363
SNMPPacket* readPacket = new SNMPPacket();
6464
for(int i = 25; i < 133; i+= 10){
6565
char old[10] = {0};
6666
memcpy(old, &buffer[i], 10);
6767
long randomLong = random();
68-
memcpy(&buffer[i], &randomLong, 10);
68+
memcpy(&buffer[i], &randomLong, sizeof(randomLong));
6969
// This may SOMETIMES fail if the random gets lucky and makes something valid
7070
REQUIRE( readPacket->parseFrom(buffer, 200) != SNMP_ERROR_OK );
7171
7272
memcpy(&buffer[i], old, 10);
7373
REQUIRE( readPacket->parseFrom(buffer, 200) == SNMP_ERROR_OK );
7474
}
75-
}
75+
} */
7676
}
7777

7878
TEST_CASE( "Test Encoding/Decoding packet", "[snmp]" ) {

0 commit comments

Comments
 (0)