85
85
86
86
87
87
void create_pdu (int , char * * , netsnmp_pdu * * , struct OIDStruct * * , int , long );
88
+ void * realloc_zero (void * , size_t , size_t );
88
89
89
90
90
91
/* hardware mode */
@@ -96,7 +97,7 @@ unsigned int lastcheck = 0;
96
97
unsigned long global_timeout = DFLT_TIMEOUT ;
97
98
98
99
static
99
- int ifNumber = 0 ;
100
+ int ifNumber = 0 , ifCount = 0 ;
100
101
101
102
#ifdef DEBUG
102
103
static
@@ -116,7 +117,7 @@ main(int argc, char *argv[])
116
117
netsnmp_pdu * pdu ;
117
118
netsnmp_pdu * response ;
118
119
119
- netsnmp_variable_list * vars ;
120
+ netsnmp_variable_list * vars , * vars2 ;
120
121
int status , status2 ;
121
122
int count = 0 ; /* used for: the number of interfaces we receive, the number of regex matches */
122
123
int loopCount = 0 ;
@@ -542,20 +543,8 @@ main(int argc, char *argv[])
542
543
vars = vars -> next_variable ;
543
544
}
544
545
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 ));
559
548
560
549
#ifdef DEBUG
561
550
fprintf (stderr , "got %d interfaces\n" , ifNumber );
@@ -564,6 +553,25 @@ main(int argc, char *argv[])
564
553
/* subsequent replies have no ifNumber */
565
554
}
566
555
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
+ }
567
575
568
576
for (vars = vars ; vars ; vars = vars -> next_variable ) {
569
577
/*
@@ -1687,3 +1695,13 @@ void create_pdu(int mode, char **oidlist, netsnmp_pdu **pdu, struct OIDStruct **
1687
1695
snmp_add_null_var (* pdu , (* oids )[i ].name , (* oids )[i ].name_len );
1688
1696
}
1689
1697
}
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