hi,

the attached patch fix a couple of leaks, check for malloc() failures and properly call smiExit() before exiting.

The remaining leak and two of three errors appears to be in libsmi, the
only error is mib2opennms is this:

==8184== Invalid write of size 1
==8184==    at 0x40231D7: strcat (mc_replace_strmem.c:186)
==8184==    by 0x804940F: main (mib2opennms.c:261)
==8184==  Address 0x41c4196 is 0 bytes after a block of size 22 alloc'd
==8184==    at 0x4021AB8: malloc (vg_replace_malloc.c:207)
==8184==    by 0x80493C6: main (mib2opennms.c:249)

It's an off by one somewhere near the strlen but i have to check more carefully.

Then i think will be fine to exit(1) on errors.

If you run with proper mibs you'll find one more leak fixed. I appreciate if you can test with a set of mibs that output is not changed.

How i checked:

valgrind --tool=memcheck --leak-check=yes ./mib2opennms -6 -f foo foobar

before:

==8142== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 13 from 1)
==8142== malloc/free: in use at exit: 2,084 bytes in 48 blocks.
==8142== malloc/free: 111 allocs, 63 frees, 6,183 bytes allocated.
==8142== For counts of detected errors, rerun with: -v
==8142== searching for pointers to 48 not-freed blocks.
==8142== checked 70,160 bytes.

==8142== LEAK SUMMARY:
==8142==    definitely lost: 103 bytes in 3 blocks.
==8142==      possibly lost: 0 bytes in 0 blocks.
==8142==    still reachable: 1,981 bytes in 45 blocks.
==8142==         suppressed: 0 bytes in 0 blocks.

after:

==8184== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 13 from 1)
==8184== malloc/free: in use at exit: 81 bytes in 2 blocks.
==8184== malloc/free: 111 allocs, 109 frees, 6,183 bytes allocated.
==8184== For counts of detected errors, rerun with: -v
==8184== searching for pointers to 2 not-freed blocks.
==8184== checked 68,224 bytes.


==8184== LEAK SUMMARY:
==8184==    definitely lost: 81 bytes in 2 blocks.
==8184==      possibly lost: 0 bytes in 0 blocks.
==8184==    still reachable: 0 bytes in 0 blocks.
==8184==         suppressed: 0 bytes in 0 blocks.

cheers,
rm
Index: mib2opennms.c
===================================================================
--- mib2opennms.c	(revisione 9249)
+++ mib2opennms.c	(copia locale)
@@ -92,7 +92,7 @@
 	return output;
 }
 
-void dumpXml(SmiModule* smiModule, FILE* file, EventDefaults* defs) {
+int dumpXml(SmiModule* smiModule, FILE* file, EventDefaults* defs) {
 	SmiNode*    smiNode;
 	SmiNode*    tmpNode;
 	SmiElement* smiElem;
@@ -133,6 +133,10 @@
 		fprintf(file, "<table>");
 
 		logmsg = (char *) malloc( 2000 * sizeof (char));
+		if (!logmsg) {
+			perror("Cannot allocate memory");
+			return 0;
+		}
 		logmsg[0]='\0';
 		sprintf(logmsg, "\t\t<logmsg dest='logndisplay'>&lt;p&gt;\n\t\t\t%s trap received ", 
 			smiNode->name);
@@ -176,6 +180,8 @@
 	}
 	fprintf(file, "<!-- End of auto generated data from MIB: %s -->\n", 
 		smiModule->name);
+
+	return 1;
 }
 
 void usage() {
@@ -204,9 +210,9 @@
 	FILE* file = stdout;
 
 	SmiModule* smiModule;
-	SmiModule** modules = NULL;
+	SmiModule** modules;
 
-	EventDefaults* defaults = NULL;
+	EventDefaults* defaults;
 
 	fprintf(stderr, "mib2opennms version %s\n", VERSION);
 
@@ -241,6 +247,10 @@
 	pathlen = strlen(STANDARD_PATH) + (mibpath == NULL ? 0 : strlen(mibpath)+1 /* for the colon */);
 
 	newpath = (char *) malloc( pathlen * sizeof(char) );
+	if (!newpath) {
+		perror("Cannot allocate memory");
+		exit(1);
+	}
 	newpath[0] = '\0';
 
 	if (mibpath != NULL) {
@@ -253,6 +263,10 @@
 	smiSetPath(newpath);
 
 	modules = (SmiModule **) malloc( argc * sizeof(SmiModule *));
+	if (!modules) {
+		perror("Cannot allocate memory");
+		goto out;
+	}
 	moduleCount = 0;
 
 	while( optind < argc ) {
@@ -287,13 +301,18 @@
 	}
 
 	defaults = (EventDefaults*) malloc(sizeof(struct EventDefaults));
+	if (!defaults) {
+		perror("Cannot allocate memory");
+		goto out1;
+	}
 	defaults->ueiPrefix = "uei.opennms.org/mib2opennms/";
 	defaults->severity  = "Indeterminate";
 
 	for ( i = 0; i < moduleCount; i++ ) {
 		smiModule = modules[i];
 		verbose(3, "Dumping %s to file\n", smiModule->name);
-		dumpXml(smiModule, file, defaults);
+		if (!dumpXml(smiModule, file, defaults))
+			break;
 	}
     
 	if ( filename != NULL ) {
@@ -302,6 +321,11 @@
 	}
 
 	free(defaults);
+out1:
+	free(modules);
+out:
+	free(newpath);
+	smiExit();
 
 	exit(0);
 }
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Please read the OpenNMS Mailing List FAQ:
http://www.opennms.org/index.php/Mailing_List_FAQ

opennms-devel mailing list

To *unsubscribe* or change your subscription options, see the bottom of this 
page:
https://lists.sourceforge.net/lists/listinfo/opennms-devel

Reply via email to