[Nagiosplug-devel] Patch to fix check_tcp memory allocation

David david at infotrek.co.uk
Tue Jan 20 14:29:04 CET 2004


check_tcp was allocating 1 byte per server expect string pointer, instead
of sizeof(char *) per pointer. Confirmed with electricfence with an
alignment of 1 - a segfault occurs.

The attached patch (against 1.3.1) fixes this problem.

Please cc me on any response, I am not on this list.

Cheers,
David

-- 
|> /+\ \| | |>

David Croft
Infotrek
-------------- next part --------------
--- clean/nagios-plugins-1.3.1/plugins/check_tcp.c	Tue Jun 10 00:56:47 2003
+++ plugins/check_tcp.c	Tue Jan 20 03:10:25 2004
@@ -187,9 +187,9 @@
 		asprintf (&SERVICE, "NNTP");
 		SEND = NULL;
 		EXPECT = NULL;
-		server_expect = realloc (server_expect, ++server_expect_count);
+		server_expect = realloc (server_expect, sizeof (char *) * (++server_expect_count));
 		asprintf (&server_expect[server_expect_count - 1], "200");
-		server_expect = realloc (server_expect, ++server_expect_count);
+		server_expect = realloc (server_expect, sizeof (char *) * (++server_expect_count));
 		asprintf (&server_expect[server_expect_count - 1], "201");
 		asprintf (&QUIT, "QUIT\r\n");
 		PROTOCOL = TCP_PROTOCOL;
@@ -209,7 +209,7 @@
 
 	/* use default expect if none listed in process_arguments() */
 	if (EXPECT && server_expect_count == 0) {
-		server_expect = malloc (++server_expect_count);
+		server_expect = malloc (sizeof (char *) * (++server_expect_count));
 		server_expect[server_expect_count - 1] = EXPECT;
 	}
 
@@ -447,9 +447,9 @@
 		case 'e': /* expect string (may be repeated) */
 			EXPECT = NULL;
 			if (server_expect_count == 0)
-				server_expect = malloc (++server_expect_count);
+				server_expect = malloc (sizeof (char *) * (++server_expect_count));
 			else
-				server_expect = realloc (server_expect, ++server_expect_count);
+				server_expect = realloc (server_expect, sizeof (char *) * (++server_expect_count));
 			server_expect[server_expect_count - 1] = optarg;
 			break;
 		case 'm':


More information about the Devel mailing list