]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
libcgroup: zero out allocated memory
authorDhaval Giani <dhaval@linux.vnet.ibm.com>
Thu, 14 Aug 2008 05:01:33 +0000 (05:01 +0000)
committerDhaval Giani <dhaval@linux.vnet.ibm.com>
Thu, 14 Aug 2008 05:01:33 +0000 (05:01 +0000)
Use calloc to allocate memory.

Should get rid of a number of silent memory corruptions.

Signed-off-by: Dhaval Giani <dhaval@linux.vnet.ibm.com>
Acked-by: Dan Smith <danms@us.ibm.com>
git-svn-id: https://libcg.svn.sourceforge.net/svnroot/libcg/trunk@142 4f4bb910-9a46-0410-90c8-c897d4f1cd53

wrapper.c

index 392bc6773da2c69091877c48af74c28a92334c5a..c53a8744d5161c5b022b360a0ca8ec9e9a617c12 100644 (file)
--- 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)