Skip to content

Commit df2ce93

Browse files
committed
Rework/Bugfix of 803f844 (interface count). Missed the fact, that there can be more than one snmp request.
1 parent 43f7b07 commit df2ce93

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

snmp_bulkget.c

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585

8686

8787
void create_pdu(int, char **, netsnmp_pdu **, struct OIDStruct **, int, long);
88+
void* realloc_zero(void*, size_t, size_t);
8889

8990

9091
/* hardware mode */
@@ -96,7 +97,7 @@ unsigned int lastcheck = 0;
9697
unsigned long global_timeout = DFLT_TIMEOUT;
9798

9899
static
99-
int ifNumber = 0;
100+
int ifNumber = 0, ifCount = 0;
100101

101102
#ifdef DEBUG
102103
static
@@ -116,7 +117,7 @@ main(int argc, char *argv[])
116117
netsnmp_pdu *pdu;
117118
netsnmp_pdu *response;
118119

119-
netsnmp_variable_list *vars;
120+
netsnmp_variable_list *vars, *vars2;
120121
int status, status2;
121122
int count = 0; /* used for: the number of interfaces we receive, the number of regex matches */
122123
int loopCount = 0;
@@ -542,20 +543,8 @@ main(int argc, char *argv[])
542543
vars = vars->next_variable;
543544
}
544545

545-
/* SNMP request result for ifNumber is not always reliable working.
546-
Sometimes it reports a wrong amount, which can lead to segfaults
547-
(not enough memory allocated for varaibles interfaces/oldperfdata).
548-
That's why we should count the real amount of interface entries. */
549-
int ifCount = 0;
550-
netsnmp_variable_list *tmpvars;
551-
for (tmpvars = vars; tmpvars; tmpvars = tmpvars->next_variable) {
552-
if (tmpvars->type == ASN_OCTET_STR) ifCount++;
553-
if ((tmpvars->name_length < OIDp[2].name_len) || (memcmp(OIDp[2].name, tmpvars->name, (tmpvars->name_length - 1) * sizeof(oid)))) break;
554-
}
555-
ifCount = (ifCount > ifNumber) ? ifCount : ifNumber;
556-
557-
interfaces = (struct ifStruct*)calloc((size_t)ifCount, sizeof(struct ifStruct));
558-
oldperfdata = (struct ifStruct*)calloc((size_t)ifCount, sizeof(struct ifStruct));
546+
interfaces = (struct ifStruct*)calloc((size_t)ifNumber, sizeof(struct ifStruct));
547+
oldperfdata = (struct ifStruct*)calloc((size_t)ifNumber, sizeof(struct ifStruct));
559548

560549
#ifdef DEBUG
561550
fprintf(stderr, "got %d interfaces\n", ifNumber);
@@ -564,6 +553,25 @@ main(int argc, char *argv[])
564553
/* subsequent replies have no ifNumber */
565554
}
566555

556+
/* SNMP request result for ifNumber is not always reliable working.
557+
Sometimes it reports a wrong amount, which can lead to segfaults
558+
(not enough memory allocated for varaibles interfaces/oldperfdata).
559+
That's why we should count the real amount of interface entries. */
560+
for (vars2 = vars; vars2; vars2 = vars2->next_variable) {
561+
if ((vars2->name_length < OIDp[2].name_len) || (memcmp(OIDp[2].name, vars2->name, (vars2->name_length - 1) * sizeof(oid)))) break;
562+
if (vars2->type == ASN_OCTET_STR) ifCount++;
563+
}
564+
565+
if (ifCount > ifNumber) {
566+
size_t newSize = (size_t)((ifCount + ifNumber) * sizeof(struct ifStruct));
567+
size_t oldSize = (size_t)(ifNumber * sizeof(struct ifStruct));
568+
interfaces = realloc_zero(interfaces, oldSize, newSize);
569+
oldperfdata = realloc_zero(oldperfdata, oldSize, newSize);
570+
#ifdef DEBUG
571+
fprintf(stderr, "got %d additional interfaces\n", (ifCount - ifNumber));
572+
#endif
573+
ifNumber = ifCount;
574+
}
567575

568576
for (vars = vars; vars; vars = vars->next_variable) {
569577
/*
@@ -1687,3 +1695,13 @@ void create_pdu(int mode, char **oidlist, netsnmp_pdu **pdu, struct OIDStruct **
16871695
snmp_add_null_var(*pdu, (*oids)[i].name, (*oids)[i].name_len);
16881696
}
16891697
}
1698+
1699+
void* realloc_zero(void* pBuffer, size_t oldSize, size_t newSize) {
1700+
void* pNew = realloc(pBuffer, newSize);
1701+
if ( newSize > oldSize && pNew ) {
1702+
size_t diff = newSize - oldSize;
1703+
void* pStart = ((char*)pNew) + oldSize;
1704+
memset(pStart, 0, diff);
1705+
}
1706+
return pNew;
1707+
}

0 commit comments

Comments
 (0)