Hi, Magnus

Based on your patch, I made a new patch. This patch depends on net-snmp5.7.2.
I made the following modifications compared with your patch:
1.add strdup2 to replace strdup;
2.modify addToList function;
3.change some NUL-terminated strings to counted-strings, but not all;

The patch is in the attachment. Please check it.

Best Regards!
zhuyj
On 07/16/2013 02:14 PM, Magnus Fromreide wrote:
On Tue, 2013-07-16 at 10:27 +0800, zhuyj wrote:
Thanks for your help!
I agree with you. It is a big task to replace all the NUL-terminated
string with counted-string.
Now I have a patch that can fix this. Would you like to merge this
patch in master of net-snmp?
If you send out the patch then I will look at it.

/MF




diff -urpN net-snmp-5.7.2/agent/mibgroup/notification/snmpNotifyTable.c net-snmp-5.7.2_new/agent/mibgroup/notification/snmpNotifyTable.c
--- net-snmp-5.7.2/agent/mibgroup/notification/snmpNotifyTable.c	2012-10-10 06:28:58.000000000 +0800
+++ net-snmp-5.7.2_new/agent/mibgroup/notification/snmpNotifyTable.c	2013-07-16 16:25:08.935027356 +0800
@@ -38,6 +38,8 @@
 #include <net-snmp/agent/mib_module_config.h>
 #include "net-snmp/agent/sysORTable.h"
 
+#include "net-snmp/library/system.h"
+
 #ifdef USING_NOTIFICATION_LOG_MIB_NOTIFICATION_LOG_MODULE
 #   include "notification-log-mib/notification_log.h"
 #endif
@@ -285,7 +287,7 @@ notifyTable_register_notifications(int m
     struct targetAddrTable_struct *ptr;
     struct targetParamTable_struct *pptr;
     struct snmpNotifyTable_data *nptr;
-    int             confirm, i;
+    int             confirm, i, bufLen;
     char            buf[SNMP_MAXBUF_SMALL];
     netsnmp_transport *t = NULL;
     struct agent_add_trap_args *args =
@@ -302,8 +304,9 @@ notifyTable_register_notifications(int m
      * XXX: START move target creation to target code 
      */
     for (i = 0; i < MAX_ENTRIES; i++) {
-        sprintf(buf, "internal%d", i);
-        if (get_addrForName(buf) == NULL && get_paramEntry(buf) == NULL)
+        bufLen = sprintf(buf, "internal%d", i);
+        if (get_addrForName2(buf, bufLen) == NULL &&
+            get_paramEntry(buf) == NULL)
             break;
     }
     if (i == MAX_ENTRIES) {
@@ -325,7 +328,8 @@ notifyTable_register_notifications(int m
         return 0;
     }
     ptr = snmpTargetAddrTable_create();
-    ptr->name = strdup(buf);
+    ptr->nameData = strdup2(buf, bufLen);
+    ptr->nameLen = bufLen;
     memcpy(ptr->tDomain, t->domain, t->domain_length * sizeof(oid));
     ptr->tDomainLen = t->domain_length;
     ptr->tAddressLen = t->remote_length;
@@ -334,8 +338,8 @@ notifyTable_register_notifications(int m
     ptr->timeout = ss->timeout / 1000;
     ptr->retryCount = ss->retries;
     SNMP_FREE(ptr->tagList);
-    ptr->tagList = strdup(ptr->name);
-    ptr->params = strdup(ptr->name);
+    ptr->tagList = strdup2(buf, bufLen);
+    ptr->params = strdup2(buf, bufLen);
     ptr->storageType = ST_READONLY;
     ptr->rowStatus = RS_ACTIVE;
     ptr->sess = ss;
diff -urpN net-snmp-5.7.2/agent/mibgroup/target/snmpTargetAddrEntry.c net-snmp-5.7.2_new/agent/mibgroup/target/snmpTargetAddrEntry.c
--- net-snmp-5.7.2/agent/mibgroup/target/snmpTargetAddrEntry.c	2012-10-10 06:28:58.000000000 +0800
+++ net-snmp-5.7.2_new/agent/mibgroup/target/snmpTargetAddrEntry.c	2013-07-16 16:25:47.271028777 +0800
@@ -22,6 +22,8 @@
 #include "snmpTargetAddrEntry.h"
 #include "util_funcs/header_generic.h"
 
+#include "net-snmp/library/system.h"
+
 #define snmpTargetAddrOIDLen 11 /*This is base+column, 
                                  * i.e. everything but index */
 
@@ -42,11 +44,12 @@ get_addrTable(void)
 }
 
 struct targetAddrTable_struct *
-get_addrForName(char *name)
+get_addrForName2(char *name, unsigned char nameLen)
 {
     struct targetAddrTable_struct *ptr;
     for (ptr = aAddrTable; ptr != NULL; ptr = ptr->next) {
-        if (ptr->name && strcmp(ptr->name, name) == 0)
+        if (ptr->nameLen == nameLen &&
+            memcmp(ptr->nameData, name, nameLen) == 0)
             return ptr;
     }
     return NULL;
@@ -67,7 +70,8 @@ snmpTargetAddrTable_create(void)
         malloc(sizeof(struct targetAddrTable_struct));
 
     if (newEntry) {
-        newEntry->name = NULL;
+        newEntry->nameData = NULL;
+        newEntry->nameLen = 0;
 
         newEntry->tDomainLen = 0;
         newEntry->tAddress = NULL;
@@ -99,7 +103,7 @@ snmpTargetAddrTable_dispose(struct targe
         snmp_close(reaped->sess);
     else
         SNMP_FREE(reaped->tAddress);
-    SNMP_FREE(reaped->name);
+    SNMP_FREE(reaped->nameData);
     SNMP_FREE(reaped->tagList);
     SNMP_FREE(reaped->params);
 
@@ -129,23 +133,19 @@ snmpTargetAddrTable_addToList(struct tar
         *listPtr = newEntry;
         return;
     } else {
-        /*
-         * get the 'OID' value of the new entry 
-         */
-        newOIDLen = strlen(newEntry->name);
-        for (i = 0; i < (int) newOIDLen; i++) {
-            newOID[i] = newEntry->name[i];
+        newOIDLen = (size_t) newEntry->nameLen; 
+        for (i = 0; i < newEntry->nameLen; i++) {
+            newOID[i] = newEntry->nameData[i];
         }
 
         /*
          * search through the list for an equal or greater OID value 
          */
         while (curr_struct != NULL) {
-            currOIDLen = strlen(curr_struct->name);
-            for (i = 0; i < (int) currOIDLen; i++) {
-                currOID[i] = curr_struct->name[i];
+            currOIDLen = (size_t)curr_struct->nameLen;
+            for (i = 0; i < curr_struct->nameLen; i++) {
+                currOID[i] = curr_struct->nameData[i];
             }
-
             i = snmp_oid_compare(newOID, newOIDLen, currOID, currOIDLen);
             if (i == 0) {       /* Exact match, overwrite with new struct */
                 newEntry->next = curr_struct->next;
@@ -233,13 +233,12 @@ search_snmpTargetAddrTable(oid * baseNam
      * lookup entry in addrTable linked list, Get Current MIB ID 
      */
     memcpy(newNum, baseName, baseNameLen * sizeof(oid));
-
     for (temp_struct = aAddrTable; temp_struct != NULL;
          temp_struct = temp_struct->next) {
-        for (i = 0; i < (int) strlen(temp_struct->name); i++) {
-            newNum[baseNameLen + i] = temp_struct->name[i];
+        for (i = 0; i < temp_struct->nameLen; i++) {
+            newNum[baseNameLen + i] = temp_struct->nameData[i];
         }
-        myOIDLen = baseNameLen + strlen(temp_struct->name);
+        myOIDLen = baseNameLen + temp_struct->nameLen;
         i = snmp_oid_compare(name, *length, newNum, myOIDLen);
         /*
          * Assumes that the linked list sorted by OID, low to high 
@@ -372,7 +371,8 @@ snmpTargetAddr_addName(struct targetAddr
                         "ERROR snmpTargetAddrEntry: name out of range in config string\n"));
             return (0);
         }
-        entry->name = strdup(cptr);
+        entry->nameData = strdup2(cptr, len);
+        entry->nameLen = len;
     }
     return (1);
 }                               /* addName */
@@ -611,7 +611,7 @@ snmpd_parse_config_targetAddr(const char
 {
     char           *cptr = char_ptr, buff[1024];
     struct targetAddrTable_struct *newEntry;
-    int             i;
+    int             i, j, buff_len;
 
     newEntry = snmpTargetAddrTable_create();
 
@@ -665,15 +665,28 @@ snmpd_parse_config_targetAddr(const char
         snmpTargetAddrTable_dispose(newEntry);
         return;
     }
-    snprintf(buff, sizeof(buff), "snmp_parse_config_targetAddr, read: %s\n",
-            newEntry->name);
+//    snprintf(buff, sizeof(buff), "snmp_parse_config_targetAddr, read: %s\n",
+//            newEntry->nameData);
+    memset(buff, 0, sizeof(buff));
+    snprintf(buff, sizeof(buff), "snmp_parse_config_targetAddr, read: ");
+    buff_len = strlen(buff);
+    for (j=0; j<newEntry->nameLen; j++){
+        buff[buff_len+j] = newEntry->nameData[j];
+    }
+    buff_len += newEntry->nameLen;
+    buff[buff_len++] = '\n';
+
     buff[ sizeof(buff)-1 ] = 0;
     for (i = 0; i < newEntry->tDomainLen; i++) {
-        snprintf(&buff[strlen(buff)], sizeof(buff)-strlen(buff)-1,
-                 ".%d", (int) newEntry->tDomain[i]);
+//        snprintf(&buff[strlen(buff)], sizeof(buff)-strlen(buff)-1,
+//                 ".%d", (int) newEntry->tDomain[i]);
+        char tmp[1024]={0};
+        snprintf(tmp, sizeof(tmp), ".%d", (int) newEntry->tDomain[i]);
+        strncpy(&buff[buff_len], tmp, strlen(tmp));
+        buff_len += strlen(tmp);
         buff[ sizeof(buff)-1 ] = 0;
     }
-    snprintf(&buff[strlen(buff)], sizeof(buff)-strlen(buff)-1,
+    snprintf(&buff[buff_len], sizeof(buff)-buff_len-1,
             " %s %d %d %s %s %d %d\n",
             newEntry->tAddress, newEntry->timeout, newEntry->retryCount,
             newEntry->tagList, newEntry->params, newEntry->storageType,
@@ -701,7 +714,7 @@ store_snmpTargetAddrEntry(int majorID, i
 {
     struct targetAddrTable_struct *curr_struct;
     char            line[1024];
-    int             i;
+    int             i, j, line_len;
 
     if ((curr_struct = aAddrTable) != NULL) {
         while (curr_struct != NULL) {
@@ -710,21 +723,32 @@ store_snmpTargetAddrEntry(int majorID, i
                 &&
                 (curr_struct->rowStatus == SNMP_ROW_ACTIVE ||
                  curr_struct->rowStatus == SNMP_ROW_NOTINSERVICE)) {
-                snprintf(line, sizeof(line),
-                        "targetAddr %s ", curr_struct->name);
+//                snprintf(line, sizeof(line),
+//                        "targetAddr %s ", curr_struct->nameData);
+                snprintf(line, sizeof(line),"targetAddr ");
+                line_len = strlen(line);
+                for (j=0; j<curr_struct->nameLen; j++){
+                    line[line_len+j] = curr_struct->nameData[j];
+                }
+                line_len += curr_struct->nameLen;
+                line[line_len++] = 32;
                 line[ sizeof(line)-1 ] = 0;
                 for (i = 0; i < curr_struct->tDomainLen; i++) {
-                    snprintf(&line[strlen(line)],
-                            sizeof(line)-strlen(line)-1, ".%i",
-                            (int) curr_struct->tDomain[i]);
+                //    snprintf(&line[line_len],
+                //            sizeof(line)-line_len-1, ".%i",
+                //            (int) curr_struct->tDomain[i]);
+                    char tmp[1024] = {0};
+                    snprintf(tmp, sizeof(tmp), ".%i", (int) curr_struct->tDomain[i]);
+                    strncpy(&line[line_len], tmp, strlen(tmp));
+                    line_len += strlen(tmp);
                     line[ sizeof(line)-1 ] = 0;
                 }
                 strlcat(line, " ", sizeof(line));
-                read_config_save_octet_string(&line[strlen(line)],
+                read_config_save_octet_string(&line[line_len],
                                               curr_struct->tAddress,
                                               curr_struct->tAddressLen);
 
-                snprintf(&line[strlen(line)], sizeof(line)-strlen(line)-1,
+                snprintf(&line[line_len], sizeof(line)-line_len-1,
                         " %i %i \"%s\" %s %i %i",
                         curr_struct->timeout,
                         curr_struct->retryCount, curr_struct->tagList,
@@ -816,7 +840,6 @@ var_snmpTargetAddrEntry(struct variable
                                                   exact)) == NULL) {
         return NULL;
     }
-
     /*
      * We found what we were looking for, either the next OID or the exact OID 
      */
@@ -1465,22 +1488,22 @@ snmpTargetAddr_createNewRow(oid * name,
         temp_struct = snmpTargetAddrTable_create();
         if (!temp_struct)
             return SNMP_ERR_GENERR;
-        temp_struct->name = (char *) malloc(newNameLen + 1);
-        if (temp_struct->name == NULL) {
+        temp_struct->nameData = (char *) malloc(newNameLen + 1);
+        if (temp_struct->nameData == NULL) {
             SNMP_FREE(temp_struct->tagList);
             SNMP_FREE(temp_struct);
             return 0;
         }
 
+        temp_struct->nameLen = newNameLen;
         for (i = 0; i < (int) newNameLen; i++) {
-            temp_struct->name[i] = (char) name[i + snmpTargetAddrOIDLen];
+            temp_struct->nameData[i] = (char) name[i + snmpTargetAddrOIDLen];
         }
 
-        temp_struct->name[newNameLen] = '\0';
+        temp_struct->nameData[newNameLen] = '\0';
         temp_struct->rowStatus = SNMP_ROW_NOTREADY;
 
         snmpTargetAddrTable_addToList(temp_struct, &aAddrTable);
-
         return 1;
     }
 
diff -urpN net-snmp-5.7.2/agent/mibgroup/target/snmpTargetAddrEntry.h net-snmp-5.7.2_new/agent/mibgroup/target/snmpTargetAddrEntry.h
--- net-snmp-5.7.2/agent/mibgroup/target/snmpTargetAddrEntry.h	2012-10-10 06:28:58.000000000 +0800
+++ net-snmp-5.7.2_new/agent/mibgroup/target/snmpTargetAddrEntry.h	2013-07-16 16:14:30.463003658 +0800
@@ -46,7 +46,8 @@ config_add_mib(SNMPv2-TM)
      * structure definitions 
      */
      struct targetAddrTable_struct {
-         char           *name;
+         char           *nameData;
+         unsigned char   nameLen;
          oid             tDomain[MAX_OID_LEN];
          int             tDomainLen;
          unsigned char  *tAddress;
@@ -74,7 +75,8 @@ config_add_mib(SNMPv2-TM)
      FindVarMethod   var_snmpTargetAddrEntry;
 
      struct targetAddrTable_struct *get_addrTable(void);
-     struct targetAddrTable_struct *get_addrForName(char *name);
+struct targetAddrTable_struct *get_addrForName2(char *name,
+                                                unsigned char nameLen);
      struct targetAddrTable_struct *snmpTargetAddrTable_create(void);
      void            snmpTargetAddrTable_add(struct targetAddrTable_struct
                                              *newEntry);
diff -urpN net-snmp-5.7.2/agent/mibgroup/target/target.c net-snmp-5.7.2_new/agent/mibgroup/target/target.c
--- net-snmp-5.7.2/agent/mibgroup/target/target.c	2012-10-10 06:28:58.000000000 +0800
+++ net-snmp-5.7.2_new/agent/mibgroup/target/target.c	2013-07-16 16:14:30.475003659 +0800
@@ -71,7 +71,7 @@ get_target_sessions(char *taglist, Targe
             (targaddrs->tDomain, targaddrs->tDomainLen, NULL, NULL) == 0) {
             snmp_log(LOG_ERR,
                      "unsupported domain for target address table entry %s\n",
-                     targaddrs->name);
+                     targaddrs->nameData);
         }
 
         /*
@@ -253,7 +253,7 @@ get_target_sessions(char *taglist, Targe
                                 snmp_log(LOG_ERR,
                                          "unsupported mpModel/secModel combo %d/%d for target %s\n",
                                          param->mpModel, param->secModel,
-                                         targaddrs->name);
+                                         targaddrs->nameData);
                                 /*
                                  * XXX: memleak 
                                  */
diff -urpN net-snmp-5.7.2/include/net-snmp/library/system.h net-snmp-5.7.2_new/include/net-snmp/library/system.h
--- net-snmp-5.7.2/include/net-snmp/library/system.h	2012-10-10 06:28:58.000000000 +0800
+++ net-snmp-5.7.2_new/include/net-snmp/library/system.h	2013-07-16 16:29:49.207037754 +0800
@@ -153,6 +153,7 @@ SOFTWARE.
 #ifndef HAVE_STRDUP
     char           *strdup(const char *);
 #endif
+    char           *strdup2(const char *src, const int len);
 #ifndef HAVE_SETENV
     NETSNMP_IMPORT
     int             setenv(const char *, const char *, int);
diff -urpN net-snmp-5.7.2/snmplib/system.c net-snmp-5.7.2_new/snmplib/system.c
--- net-snmp-5.7.2/snmplib/system.c	2012-10-10 06:28:58.000000000 +0800
+++ net-snmp-5.7.2_new/snmplib/system.c	2013-07-16 16:29:16.439036542 +0800
@@ -1053,8 +1053,23 @@ strdup(const char *src)
     strcpy(dst, src);
     return (dst);
 }
+
 #endif                          /* HAVE_STRDUP */
 
+char * strdup2(const char *src, const int len)
+{
+    char           *dst;
+    int i;
+
+    if ((dst = (char *) malloc(len+1)) == NULL)
+        return (NULL);
+    memset(dst, 0, len+1);
+    for (i=0; i<len; i++){
+        dst[i] = src[i];
+    }
+    return (dst);
+}
+
 #ifndef HAVE_SETENV
 int
 setenv(const char *name, const char *value, int overwrite)
------------------------------------------------------------------------------
See everything from the browser to the database with AppDynamics
Get end-to-end visibility with application monitoring from AppDynamics
Isolate bottlenecks and diagnose root cause in seconds.
Start your free trial of AppDynamics Pro today!
http://pubads.g.doubleclick.net/gampad/clk?id=48808831&iu=/4140/ostg.clktrk
_______________________________________________
Net-snmp-coders mailing list
Net-snmp-coders@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

Reply via email to