From: Dhaval Giani Date: Thu, 14 Aug 2008 05:01:33 +0000 (+0000) Subject: libcgroup: zero out allocated memory X-Git-Tag: v0.34~245 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1feaaa075ad581968636277a2b01b3e429cd243f;p=thirdparty%2Flibcgroup.git libcgroup: zero out allocated memory Use calloc to allocate memory. Should get rid of a number of silent memory corruptions. Signed-off-by: Dhaval Giani Acked-by: Dan Smith git-svn-id: https://libcg.svn.sourceforge.net/svnroot/libcg/trunk@142 4f4bb910-9a46-0410-90c8-c897d4f1cd53 --- diff --git a/wrapper.c b/wrapper.c index 392bc677..c53a8744 100644 --- a/wrapper.c +++ b/wrapper.c @@ -25,8 +25,7 @@ struct cgroup *cgroup_new_cgroup(const char *name) { - struct cgroup *cgroup = (struct cgroup *) - malloc(sizeof(struct cgroup)); + struct cgroup *cgroup = calloc(1, sizeof(struct cgroup)); if (!cgroup) return NULL; @@ -57,8 +56,7 @@ struct cgroup_controller *cgroup_add_controller(struct cgroup *cgroup, return NULL; } - controller = (struct cgroup_controller *) - malloc(sizeof(struct cgroup_controller)); + controller = calloc(1, sizeof(struct cgroup_controller)); if (!controller) return NULL; @@ -97,11 +95,7 @@ int cgroup_add_value_string(struct cgroup_controller *controller, const char *name, const char *value) { int i; - struct control_value *cntl_value = (struct control_value *) - malloc(sizeof(struct control_value)); - - if (!cntl_value) - return ECGCONTROLLERCREATEFAILED; + struct control_value *cntl_value; if (controller->index >= CG_VALUE_MAX) return ECGMAXVALUESEXCEEDED; @@ -111,6 +105,10 @@ int cgroup_add_value_string(struct cgroup_controller *controller, return ECGVALUEEXISTS; } + cntl_value = calloc(1, sizeof(struct control_value)); + + if (!cntl_value) + return ECGCONTROLLERCREATEFAILED; strncpy(cntl_value->name, name, sizeof(cntl_value->name)); strncpy(cntl_value->value, value, sizeof(cntl_value->value)); @@ -125,12 +123,7 @@ int cgroup_add_value_int64(struct cgroup_controller *controller, { int i; unsigned ret; - struct control_value *cntl_value = (struct control_value *) - malloc(sizeof(struct control_value)); - - if (!cntl_value) - return ECGCONTROLLERCREATEFAILED; - + struct control_value *cntl_value; if (controller->index >= CG_VALUE_MAX) return ECGMAXVALUESEXCEEDED; @@ -140,6 +133,11 @@ int cgroup_add_value_int64(struct cgroup_controller *controller, return ECGVALUEEXISTS; } + cntl_value = calloc(1, sizeof(struct control_value)); + + if (!cntl_value) + return ECGCONTROLLERCREATEFAILED; + strncpy(cntl_value->name, name, sizeof(cntl_value->name)); ret = snprintf(cntl_value->value, @@ -160,12 +158,7 @@ int cgroup_add_value_uint64(struct cgroup_controller *controller, { int i; unsigned ret; - struct control_value *cntl_value = (struct control_value *) - malloc(sizeof(struct control_value)); - - if (!cntl_value) - return ECGCONTROLLERCREATEFAILED; - + struct control_value *cntl_value; if (controller->index >= CG_VALUE_MAX) return ECGMAXVALUESEXCEEDED; @@ -175,6 +168,11 @@ int cgroup_add_value_uint64(struct cgroup_controller *controller, return ECGVALUEEXISTS; } + cntl_value = calloc(1, sizeof(struct control_value)); + + if (!cntl_value) + return ECGCONTROLLERCREATEFAILED; + strncpy(cntl_value->name, name, sizeof(cntl_value->name)); ret = snprintf(cntl_value->value, sizeof(cntl_value->value), "%lu", value); @@ -194,12 +192,7 @@ int cgroup_add_value_bool(struct cgroup_controller *controller, { int i; unsigned ret; - struct control_value *cntl_value = (struct control_value *) - malloc(sizeof(struct control_value)); - - if (!cntl_value) - return ECGCONTROLLERCREATEFAILED; - + struct control_value *cntl_value; if (controller->index >= CG_VALUE_MAX) return ECGMAXVALUESEXCEEDED; @@ -209,6 +202,11 @@ int cgroup_add_value_bool(struct cgroup_controller *controller, return ECGVALUEEXISTS; } + cntl_value = calloc(1, sizeof(struct control_value)); + + if (!cntl_value) + return ECGCONTROLLERCREATEFAILED; + strncpy(cntl_value->name, name, sizeof(cntl_value->name)); if (value)