[icinga-checkins] icinga.org: icinga-core/mfriedrich/core: core: fix having empty host/ servicegroups, $HOST|SERVICEGROUPMEMBERS$ on-demand macro returns garbage on realloc ( thx roguelazer) #2913 - MF

git at icinga.org git at icinga.org
Wed Aug 22 21:20:17 CEST 2012


Module: icinga-core
Branch: mfriedrich/core
Commit: eea73bf349456b37155d1d5d7786977fba4eec11
URL:    https://git.icinga.org/?p=icinga-core.git;a=commit;h=eea73bf349456b37155d1d5d7786977fba4eec11

Author: Michael Friedrich <michael.friedrich at univie.ac.at>
Date:   Wed Aug 22 21:19:40 2012 +0200

core: fix having empty host/servicegroups, $HOST|SERVICEGROUPMEMBERS$ on-demand macro returns garbage on realloc (thx roguelazer) #2913 - MF

refs #2913

---

 Changelog          |    1 +
 common/macros.c    |   14 ++++++++++
 tests/etc/2913.cfg |   71 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+), 0 deletions(-)

diff --git a/Changelog b/Changelog
index bc435f8..f47e060 100644
--- a/Changelog
+++ b/Changelog
@@ -71,6 +71,7 @@ FIXES
 * core: avoid duplicate events when scheduling forced host|service check (Imri Zvik) #2993
 * core: unknown macros are not replaced, and misleading to single dollar signs #2291 - MF
 	** you can revert to the old behaviour with keep_unknown_macros=1 in icinga.cfg
+* core: fix having empty host/servicegroups, $HOST|SERVICEGROUPMEMBERS$ on-demand macro returns garbage on realloc (thx roguelazer) #2913 - MF
 
 * classic ui: fix setting send_notification or sticky_ack as GET param has no effect on cmd.cgi acks #2926 - MF
 	** now you can finally disable checkboxes default ticked with these options
diff --git a/common/macros.c b/common/macros.c
index 6e5b6c9..7c37581 100644
--- a/common/macros.c
+++ b/common/macros.c
@@ -1971,13 +1971,20 @@ int grab_standard_hostgroup_macro_r(icinga_macros *mac, int macro_type, hostgrou
 				temp_len += strlen(temp_hostsmember->host_name) + 2;
 			}
 		}
+
+		/* if there no hostgroups members, we need to malloc at least an empty string for future calls of this function */
+		if (temp_len <= 0)
+			temp_len = 1;
+
 		/* allocate or reallocate the memory buffer */
 		if (*output == NULL) {
 			*output = (char *)malloc(temp_len);
+			(*output)[temp_len-1] = '\0';
 		} else {
 			init_len = strlen(*output);
 			temp_len += init_len;
 			*output = (char *)realloc(*output, temp_len);
+			(*output)[temp_len-1] = '\0';
 		}
 		/* now fill in the string with the member names */
 		for (temp_hostsmember = temp_hostgroup->members; temp_hostsmember != NULL; temp_hostsmember = temp_hostsmember->next) {
@@ -2306,13 +2313,20 @@ int grab_standard_servicegroup_macro_r(icinga_macros *mac, int macro_type, servi
 				temp_len += strlen(temp_servicesmember->host_name) + strlen(temp_servicesmember->service_description) + 3;
 			}
 		}
+
+		/* if there no hostgroups members, we need to malloc at least an empty string for future calls of this function */
+		if (temp_len <= 0)
+			temp_len = 1;
+
 		/* allocate or reallocate the memory buffer */
 		if (*output == NULL) {
 			*output = (char *)malloc(temp_len);
+			(*output)[temp_len-1] = '\0';
 		} else {
 			init_len = strlen(*output);
 			temp_len += init_len;
 			*output = (char *)realloc(*output, temp_len);
+			(*output)[temp_len-1] = '\0';
 		}
 		/* now fill in the string with the group members */
 		for (temp_servicesmember = temp_servicegroup->members; temp_servicesmember != NULL; temp_servicesmember = temp_servicesmember->next) {
diff --git a/tests/etc/2913.cfg b/tests/etc/2913.cfg
new file mode 100644
index 0000000..7bfeada
--- /dev/null
+++ b/tests/etc/2913.cfg
@@ -0,0 +1,71 @@
+#############################################################################################
+# ICINGA TEST CONFIG BY ISSUES
+# (c) 2009-2012 Icinga Development Team and Community Contributors
+#
+# #2913
+# with empty hostgroups, $HOSTGROUPMEMBERS$ returns garbage
+#############################################################################################
+
+define hostgroup {
+        hostgroup_name  		2913linux-servers
+        alias   			2913 Linux Servers
+#        members 			2913localhost
+}
+
+define command{
+	command_name			2913macrotest
+	command_line			/bin/echo $HOSTGROUPMEMBERS:2913linux-servers$
+}
+
+define host {
+        host_name       		2913localhost
+        alias   			2913localhost
+        address 			127.0.0.1
+        check_period    		24x7
+        check_command   		2913macrotest
+        contact_groups  		testconfig-group-admin
+        notification_period     	workhours
+        initial_state   		o
+        check_interval  		5.000000
+        retry_interval  		1.000000
+        max_check_attempts      	10
+        active_checks_enabled   	1
+        passive_checks_enabled  	1
+        obsess_over_host        	1
+        event_handler_enabled   	1
+        low_flap_threshold      	0.000000
+        high_flap_threshold     	0.000000
+        flap_detection_enabled  	1
+        flap_detection_options  	o,d,u
+        freshness_threshold     	0
+        check_freshness 		0
+        notification_options    	d,u,r
+        notifications_enabled   	1
+        notification_interval   	120.000000
+        first_notification_delay        0.000000
+        stalking_options        	n
+        process_perf_data       	1
+        failure_prediction_enabled      1
+        retain_status_information       1
+        retain_nonstatus_information    1
+}
+
+define servicegroup {
+        servicegroup_name               2913servicegroup
+        alias                           2913 Service Group
+        #members                                2913localhost,2913service
+}
+
+define command{
+        command_name                    2913macrotest2
+        command_line                    /bin/echo $SERVICEGROUPMEMBERS:2913servicegroup$
+}
+
+
+define service {
+        service_description             2913service
+        host_name                       2913localhost
+        check_command                   2913macrotest2
+        use                             generic-service
+}
+





More information about the icinga-checkins mailing list