]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
cgrulesengd: Improve handling of out-of-memory.
authorNelson Elhage <nelhage@ksplice.com>
Fri, 18 Feb 2011 01:55:11 +0000 (20:55 -0500)
committerJan Safranek <jsafrane@redhat.com>
Wed, 23 Feb 2011 12:28:27 +0000 (13:28 +0100)
Don't leak the old array if the allocation fails, and don't touch num_allocation
if the allocation fails.

Signed-off-by: Nelson Elhage <nelhage@ksplice.com>
Signed-off-by: Jan Safranek <jsafrane@redhat.com>
src/daemon/cgrulesengd.c

index a2f9549e3e3996d552322769fa6629c9c760ed8f..10fd2cd7322709365d80947a104c47f75b6f2f11 100644 (file)
@@ -173,13 +173,15 @@ static int cgre_store_parent_info(pid_t pid)
        uptime_ns = ((__u64)tp.tv_sec * 1000 * 1000 * 1000 ) + tp.tv_nsec;
 
        if (array_pi.index >= array_pi.num_allocation) {
-               array_pi.num_allocation += NUM_PER_REALLOCATIOM;
-               array_pi.parent_info = realloc(array_pi.parent_info,
-                                       sizeof(info) * array_pi.num_allocation);
-               if (!array_pi.parent_info) {
+               int alloc = array_pi.num_allocation + NUM_PER_REALLOCATIOM;
+               void *new_array = realloc(array_pi.parent_info,
+                                         sizeof(info) * alloc);
+               if (!new_array) {
                        flog(LOG_WARNING, "Failed to allocate memory");
                        return 1;
                }
+               array_pi.parent_info = new_array;
+               array_pi.num_allocation = alloc;
        }
        info = calloc(1, sizeof(struct parent_info));
        if (!info) {
@@ -258,13 +260,15 @@ static int cgre_store_unchanged_process(pid_t pid, int flags)
                return 0;
        }
        if (array_unch.index >= array_unch.num_allocation) {
-               array_unch.num_allocation += NUM_PER_REALLOCATIOM;
-               array_unch.proc = realloc(array_unch.proc,
-                       sizeof(unchanged_pid_t) * array_unch.num_allocation);
-               if (!array_unch.proc) {
+               int alloc = array_unch.num_allocation + NUM_PER_REALLOCATIOM;
+               void *new_array = realloc(array_unch.proc,
+                                         sizeof(unchanged_pid_t) * alloc);
+               if (!new_array) {
                        flog(LOG_WARNING, "Failed to allocate memory");
                        return 1;
                }
+               array_unch.proc = new_array;
+               array_unch.num_allocation = alloc;
        }
        array_unch.proc[array_unch.index].pid = pid;
        array_unch.proc[array_unch.index].flags = flags;