[icinga-checkins] icinga.org: icinga-core/rbartels/1.3: test_checks: Use calloc(1, x) instead of malloc(x) (Andreas Ericsson)

git at icinga.org git at icinga.org
Thu Nov 11 19:54:25 CET 2010


Module: icinga-core
Branch: rbartels/1.3
Commit: 9f4624bd3ba6237c531f0c9705c8f07d80e0caf8
URL:    https://git.icinga.org/?p=icinga-core.git;a=commit;h=9f4624bd3ba6237c531f0c9705c8f07d80e0caf8

Author: Michael Friedrich <michael.friedrich at univie.ac.at>
Date:   Mon Oct 25 15:38:23 2010 +0200

test_checks: Use calloc(1, x) instead of malloc(x) (Andreas Ericsson)

This prevents a segfault which otherwise occurs when we pass the
service struct to handle_async_service_check_result() with a
non-NULL but invalid pointer to the plugin_output() variable.

Using malloc() in shortlived programs is almost always an error.

Signed-off-by: Andreas Ericsson <ae at op5.se>

Author: Andreas Ericsson <ae at op5.se>

---

 t-tap/test_checks.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/t-tap/test_checks.c b/t-tap/test_checks.c
index 74a9977..d46460e 100644
--- a/t-tap/test_checks.c
+++ b/t-tap/test_checks.c
@@ -217,13 +217,13 @@ void
 setup_objects(time_t time) {
 	timed_event *new_event=NULL;
 
-	host1=(host *)malloc(sizeof(host));
+	host1=(host *)calloc(1, sizeof(host));
 	host1->current_state=HOST_DOWN;
 	host1->has_been_checked=TRUE;
 	host1->last_check=time;
 
 	/* First service is a normal one */
-	svc1=(service *)malloc(sizeof(service));
+	svc1=(service *)calloc(1, sizeof(service));
 	svc1->host_name=strdup("Host1");
 	svc1->host_ptr=host1;
 	svc1->description=strdup("Normal service");
@@ -237,7 +237,7 @@ setup_objects(time_t time) {
 	svc1->last_state_change=0;
 
 	/* Second service .... to be configured! */
-	svc2=(service *)malloc(sizeof(service));
+	svc2=(service *)calloc(1, sizeof(service));
 	svc2->host_name=strdup("Host1");
 	svc2->description=strdup("To be nudged");
 	svc2->check_options=0;
@@ -258,7 +258,7 @@ main (int argc, char **argv){
 	time(&now);
 
 	/* Test to confirm that if a service is warning, the notified_on_critical is reset */
-	tmp_check_result=(check_result *)malloc(sizeof(check_result));
+	tmp_check_result=(check_result *)calloc(1, sizeof(check_result));
 	tmp_check_result->host_name=strdup("host1");
 	tmp_check_result->service_description=strdup("Normal service");
 	tmp_check_result->object_check_type=SERVICE_CHECK;





More information about the icinga-checkins mailing list