Skip to content

Commit

Permalink
Fix crash with long OID (> 50).
Browse files Browse the repository at this point in the history
MAX_OID_LENGTH 128
  • Loading branch information
shortbloke committed Sep 11, 2021
1 parent 2adad9a commit ca5c04f
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 13 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# CHANGELOG for SNMP Manager For ESP8266/ESP32/Arduino

## 1.1.5

- Support longer OIDs. Change in v1.1.1 was incomplete

## 1.1.4

- Fixes #12 where additional check for packet length was incorrect and unnecessary.
- Fixes #12 where additional check for packet length was incorrect and unnecessary

## 1.1.3

Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=SNMP Manager
version=1.1.4
version=1.1.5
author=Martin Rowan <martin@martinrowan.co.uk>
maintainer=Martin Rowan <martin@martinrowan.co.uk>
sentence=An SNMP Manager library to make SNMP requests to other SNMP enabled devices.
Expand Down
8 changes: 3 additions & 5 deletions src/Arduino_SNMP_Manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
#ifndef SNMP_PACKET_LENGTH
#if defined(ESP32)
#define SNMP_PACKET_LENGTH 1500 // This will limit the size of packets which can be handled.
#elif defined(ESP8266)
#define SNMP_PACKET_LENGTH 512 // This will limit the size of packets which can be handled. ESP8266 is unstable and crashes as this value approaches or exceeds1024. This appears to be a problem in the underlying WiFi or UDP implementation
#else
#define SNMP_PACKET_LENGTH 484 // This value may need to be made smaller for lower memory devices. This will limit the size of packets which can be handled.
#define SNMP_PACKET_LENGTH 512 // This value may need to be made smaller for lower memory devices.
#endif
#endif

Expand Down Expand Up @@ -120,7 +118,7 @@ class SNMPManager
bool begin();
bool loop();
bool testParsePacket(String testPacket);
char OIDBuf[50];
char OIDBuf[MAX_OID_LENGTH];
UDP *_udp;
void addHandler(ValueCallback *callback);

Expand Down Expand Up @@ -415,7 +413,7 @@ ValueCallback *SNMPManager::findCallback(IPAddress ip, const char *oid)
{
while (true)
{
memset(OIDBuf, 0, 50);
memset(OIDBuf, 0, MAX_OID_LENGTH);
strcat(OIDBuf, callbacksCursor->value->OID);
if ((strcmp(OIDBuf, oid) == 0) && (callbacksCursor->value->ip == ip))
{
Expand Down
8 changes: 6 additions & 2 deletions src/BER.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#define SNMP_OCTETSTRING_MAX_LENGTH 1024
#endif

#ifndef MAX_OID_LENGTH
#define MAX_OID_LENGTH 128
#endif

#include <Arduino.h>
#include <math.h>

Expand Down Expand Up @@ -275,10 +279,10 @@ class OIDType : public BER_CONTAINER
OIDType() : BER_CONTAINER(true, OID){};
OIDType(char *value) : BER_CONTAINER(true, OID)
{
strncpy(_value, value, 128);
strncpy(_value, value, MAX_OID_LENGTH);
};
~OIDType(){};
char _value[128];
char _value[MAX_OID_LENGTH];
int serialise(unsigned char *buf)
{
#ifdef DEBUG_BER
Expand Down
2 changes: 1 addition & 1 deletion src/SNMPGet.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class SNMPGet
int length = packet->serialise(_packetBuffer);
delete packet;
packet = 0;
_udp->beginPacket(ip, 161); // MFR: Trap 162, Get 161
_udp->beginPacket(ip, 161);
_udp->write(_packetBuffer, length);
return _udp->endPacket();
}
Expand Down
4 changes: 1 addition & 3 deletions src/SNMPGetResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class SNMPGetResponse

bool SNMPGetResponse::parseFrom(unsigned char *buf)
{
// SNMPPacket = new ComplexType(STRUCTURE); // ensure SNMPPacket is initialised to avoid crash in deconstructor
// confirm that the packet is a STRUCTURE
if (buf[0] != 0x30)
{
Expand Down Expand Up @@ -82,7 +81,7 @@ bool SNMPGetResponse::parseFrom(unsigned char *buf)
return false;
}
cursor = cursor->next;
EXPECTING = PDU; // temp
EXPECTING = PDU;
}
else
{
Expand Down Expand Up @@ -191,7 +190,6 @@ bool SNMPGetResponse::parseFrom(unsigned char *buf)
}
else
{
// tempCursor = ((ComplexType*)cursor->next->value)->_values;
tempCursor = tempCursor->next;
varBindsCursor = varBindsCursor->next;
}
Expand Down

0 comments on commit ca5c04f

Please sign in to comment.